How to past the function getYTD("ANL") to Index page in controller and call back in View?
What you are asking for doesn't seem to be a good idea.
Tightly coupling your view with your repository is not a gud idea. view is not supposed to know about anything else than about it's model. This is MVC.
So alternative would be,
If you Model typeo of your index view is of LeaveInfo then you can actually create a model object in your index action and supply that model to your view as.
public ViewResult Index()
{
var model=getYTD("someleaveid");
ViewBag.LeaveTypeID = new SelectList(db.LeaveType, "LeaveTypeID", "LeaveTypeName");
return View(model);
}
but if your model is not LeaveInfo then you can still use your ViewBag to store that object.
But supplying a function pointer to the view is not a good idea.
Thank you very much for the useful & informative info about MVC. I am new on MVC .Net.
If i would like to change to
public void getYTD(string lty)
{
string prefix = System.Threading.Thread.CurrentPrincipal.Identity.Name;
string selectSQL = "SELECT openBal=Isnull(openBal+BF,0) FROM LeaveSumm LS inner join empAllocateB ab on LS.[EmpAllocateBridgeID]=ab.[EmpAllocateBridgeID] where [year]='2011' and ab.EmpAllocateBridgeID=2 and ab.EmployeeID=(select e.EmployeeID from
emp e inner join empAllocateBrid al on al.EmployeeID=e.EmployeeID inner join LeaveAlloc pid on pid.[LeaveAllocateByPosID]=al.[LeaveAllocateByPosID] where prefix='" + prefix + "' and pid.LeaveTypeID=(select [LeaveTypeID] from LeaveTypes where [LeaveTypeCode]='"
+ lty + "'))";
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
How to call in index view page or do you have any reference on how to call external function. Your suggestion above only allow return 1 data. But i have few function need to call out.
My whole point was that MVC pattern doesn't allow your view to have any knowledge about your service layer or dao.
So if you need any object or data in your view then you get those data in your action and store them in your viewbag or viewdata or create a suitable viewmodel for that. Then in your view you just consume these viewbag or viewdata or get the required data
from your viewmodel.
how would the view know the value to pass to getYTD, unless the controller passed it? but anyway to pass a function to the view, just add it to the model as a method.
remember the view has no data that is not passed to it by the controller. so the controller should just normally pre-calc information.
I try to do this, but still failed. Please help me to look on below code. I really stuck here. I try to search resources online, but can't get what i need. Please HELP me.
string prefix = System.Threading.Thread.CurrentPrincipal.Identity.Name;
string selectSQL = "SELECT openBal=Isnull(openBal+BF,0) FROM LeaveSumm LS
inner join empAllocateBrid ab on LS.[EmpAllocateBridgeID]=ab.[EmpAllocateBridgeID]
where [year]='2011' and ab.EmpAllocateBridgeID=2
and ab.EmployeeID='"+prefix+"'";
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
var model = new LeaveInfo();
try
{
con.Open();
reader = cmd.ExecuteReader();
micnie2020
Member
306 Points
524 Posts
How to past controller customize function into Index View Page
Sep 15, 2011 03:56 AM|LINK
Hi,
How to past the function getYTD("ANL") to Index page in controller and call back in View? My code as below:-
public class HomeController : Controller
{
public LeaveInfo getYTD(string lty)
{
string selectSQL = "SELECT OpenBalance =Isnull(openBal+BF,0) FROM LeaveSummaries where [year]='2011' and prefix='" + prefix + "'";
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
var model = new LeaveInfo();
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
model.OpenBalance = int.Parse(reader["OpenBalance "].ToString());
}
reader.Close();
}
finally
{
con.Close();
}
return model;
}
public ViewResult Index()
{
ViewBag.LeaveTypeID = new SelectList(db.LeaveType, "LeaveTypeID", "LeaveTypeName");
return View();
}
Please Advise.
Thank you.
Regards,
cyberbud
Contributor
3298 Points
551 Posts
Re: How to past controller customize function into Index View Page
Sep 15, 2011 04:20 AM|LINK
What you are asking for doesn't seem to be a good idea.
Tightly coupling your view with your repository is not a gud idea. view is not supposed to know about anything else than about it's model. This is MVC.
So alternative would be,
If you Model typeo of your index view is of LeaveInfo then you can actually create a model object in your index action and supply that model to your view as.
public ViewResult Index()
{
var model=getYTD("someleaveid");
ViewBag.LeaveTypeID = new SelectList(db.LeaveType, "LeaveTypeID", "LeaveTypeName");
return View(model);
}
but if your model is not LeaveInfo then you can still use your ViewBag to store that object.
But supplying a function pointer to the view is not a good idea.
MCP(.net 3.5)
Nepal
ishwor.cyberbudsonline.com
micnie2020
Member
306 Points
524 Posts
Re: How to past controller customize function into Index View Page
Sep 15, 2011 05:21 AM|LINK
Hi,
Thank you very much for the useful & informative info about MVC. I am new on MVC .Net.
If i would like to change to
public void getYTD(string lty)
{
string prefix = System.Threading.Thread.CurrentPrincipal.Identity.Name;
string selectSQL = "SELECT openBal=Isnull(openBal+BF,0) FROM LeaveSumm LS inner join empAllocateB ab on LS.[EmpAllocateBridgeID]=ab.[EmpAllocateBridgeID] where [year]='2011' and ab.EmpAllocateBridgeID=2 and ab.EmployeeID=(select e.EmployeeID from emp e inner join empAllocateBrid al on al.EmployeeID=e.EmployeeID inner join LeaveAlloc pid on pid.[LeaveAllocateByPosID]=al.[LeaveAllocateByPosID] where prefix='" + prefix + "' and pid.LeaveTypeID=(select [LeaveTypeID] from LeaveTypes where [LeaveTypeCode]='" + lty + "'))";
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
var model= OpenBalance();
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
OpenBalance=int.Parse(reader["openBal"].ToString());
}
reader.Close();
}
finally
{
con.Close();
}
return model;
}
How to call in index view page or do you have any reference on how to call external function. Your suggestion above only allow return 1 data. But i have few function need to call out.
Your help highly appreciate & Big thanks.
Regards,
Micheale
cyberbud
Contributor
3298 Points
551 Posts
Re: How to past controller customize function into Index View Page
Sep 15, 2011 05:39 AM|LINK
My whole point was that MVC pattern doesn't allow your view to have any knowledge about your service layer or dao.
So if you need any object or data in your view then you get those data in your action and store them in your viewbag or viewdata or create a suitable viewmodel for that. Then in your view you just consume these viewbag or viewdata or get the required data from your viewmodel.
MCP(.net 3.5)
Nepal
ishwor.cyberbudsonline.com
bruce (sqlwo...
All-Star
36882 Points
5451 Posts
Re: How to past controller customize function into Index View Page
Sep 15, 2011 06:00 AM|LINK
how would the view know the value to pass to getYTD, unless the controller passed it? but anyway to pass a function to the view, just add it to the model as a method.
remember the view has no data that is not passed to it by the controller. so the controller should just normally pre-calc information.
micnie2020
Member
306 Points
524 Posts
Re: How to past controller customize function into Index View Page
Sep 15, 2011 06:41 AM|LINK
Hi,
I try to do this, but still failed. Please help me to look on below code. I really stuck here. I try to search resources online, but can't get what i need. Please HELP me.
Thank you.
Controller > HomeController
---------------------------
public class HomeController : Controller
{
public LeaveInfo getYTD(string lty)
{
string prefix = System.Threading.Thread.CurrentPrincipal.Identity.Name;
string selectSQL = "SELECT openBal=Isnull(openBal+BF,0) FROM LeaveSumm LS
inner join empAllocateBrid ab on LS.[EmpAllocateBridgeID]=ab.[EmpAllocateBridgeID]
where [year]='2011' and ab.EmpAllocateBridgeID=2
and ab.EmployeeID='"+prefix+"'";
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
var model = new LeaveInfo();
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
model.OpenBalance = int.Parse(reader["openBal"].ToString());
}
reader.Close();
}
finally
{
con.Close();
}
return model;
}
public ViewResult Index()
{
string UserToView = Request.Params["Name"];
if (UserToView == null) { UserToView = "ANL"; }
var leave=getYTD(UserToView);
ViewBag.OpenBalance = leave;
return view()
}
Model
-----
public class LeaveInfo
{
[Required]
[RegularExpression(@"[a-zA-Z]+")]
public string Name { get; set; }
[Range(20, 50)]
public float OpenBalance { get; set; }
}
View > Home >Index.cshtml
--------------------------
<tr style="color: #0DA068">
<td>Available</td><td>@(Model.OpenBalance)</td>
</tr>
cyberbud
Contributor
3298 Points
551 Posts
Re: How to past controller customize function into Index View Page
Sep 15, 2011 07:53 AM|LINK
Here...this might work for you.
public ViewResult Index() { string UserToView = Request.Params["Name"]; if (UserToView == null) { UserToView = "ANL"; } var leave=getYTD(UserToView); ViewBag.OpenBalance = leave.OpenBalance; return view() } View > Home >Index.cshtml -------------------------- <tr style="color: #0DA068"> <td>Available</td><td>@(ViewBag.OpenBalance)</td> </tr>MCP(.net 3.5)
Nepal
ishwor.cyberbudsonline.com
micnie2020
Member
306 Points
524 Posts
Re: How to past controller customize function into Index View Page
Sep 15, 2011 08:16 AM|LINK
Dear Cyberbud,
Wow. It's work find for me. I have been cracking my head since 3 day and get into insomia. You are my angel for today. ;)
Thank you so much.
Blessed Day!
Regards,
Micheale