InterviewSolution
| 1. |
What are various ActionResults in MVC? |
|
Answer» When a programmer send the request and when it GOES to the action method it sees the return type of the action result and produces the desired result on the view after that, Action Result is a result of action methods or return types of action methods. Action result is an abstract class. Different types of ActionResult are-
1.View Result: View Result is very simple Action Result type it is a class which is derived from the ViewResultBase class and viewResultBase Class is derived from the ActionResult, View results in the data in view which is defined in the model. View PAGE is a simple HTML page. Here view page has “.cshtml” extension Eg, Public ViewResult About() { ViewBag.Message=”The View Result is here”; Return View(); }2.Partial View Result : The partial view is a class which is also derived from action result class. When we create a partial view it is created in the shared folder with the .cshtml extensions. If we don't have the partial view we cannot access the partial view, Partial view is one of the views that we can call inside the Normal view page. Eg. public partialViewResult Index() { Return PartialView(“_PartialView”); }3.Render Result: it helps in REDIRECTING the request to the specific url ,if the url is wrong then it gives the Error 404 Eg public RedirectResult Index() { return Redirect("Home/Contact"); }4.Redirect to Action Result: it helps in redirecting the request to action through the controller it is not necessary to mention the controller name in RedirectToAction() if controller is not there then it will redirected to the action which matches in RedirectToAction() ,if action does not find on the page then it will give the error 404. public ActionResult Index() { return RedirectToAction("Login", "Account"); }5.Json Result : Json result is a SIGNIFICANT Action Result in MVC. It will return response in the format of key value pairs.it return json if we call json method Eg: Public ActionResult Index() { var persons = new List<Person1> { new Person1 { Id=1, FirstName="Harry", LastName="Potter" }, new Person1 { Id=2, FirstName="James", LastName="Raj" } }; return Json(persons, JsonRequestBehavior.AllowGet); } |
|