Wednesday 7 October 2015

Other Important Points on MVC

Can we map multiple URL’s to the same action?
Yes , you can , you just need to make two entries with different key names and specify the same controller
and action.
How can we navigate from one view to other view using hyperlink?
By using “ActionLink” method as shown in the below code. The below code will create a simple URL
which help to navigate to the “Home” controller and invoke the “GotoHome” action.
<%= Html.ActionLink("Home","Gotohome") %>
How can we restrict MVC actions to be invoked only by GET or POST?
We can decorate the MVC action by “HttpGet” or “HttpPost” attribute to restrict the type of HTTP calls.
For instance you can see in the below code snippet the “DisplayCustomer” action can only be invoked by
“HttpGet”. If we try to make Http post on “DisplayCustomer” it will throw an error.
[HttpGet]
public ViewResult DisplayCustomer(int id)
{
Customer objCustomer = Customers[id];
return View("DisplayCustomer",objCustomer);
}


What is the use of Keep and Peek in “TempData”?
Once “TempData” is read in the current request it’s not available in the subsequent request. If we want
“TempData” to be read and also available in the subsequent request then after reading we need to call
“Keep” method as shown in the code below.
@TempData[“MyData”];
TempData.Keep(“MyData”);
The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well
advices MVC to maintain “TempData” for the subsequent request.
string str = TempData.Peek("Td").ToString();

No comments:

Post a Comment