Types of Action
Results
There are different Types of action results in ASP.NET MVC. Each result has a different type of result format to view page.
There are different Types of action results in ASP.NET MVC. Each result has a different type of result format to view page.
- View Result
- Partial View Result
- Redirect Result
- Redirect To Action Result
- Redirect To Route Result
- Json Result
- File Result
- Content Result
View Result
View result is a basic view result. It returns basic results to view page. View result can return data to view page through which class is defined in the model. View page is a simple HTML page.
View result is a basic view result. It returns basic results to view page. View result can return data to view page through which class is defined in the model. View page is a simple HTML page.
public ViewResult About(){
ViewBag.Message = "Your application description page.";
return View();
}
Partial View Result
Partial View Result is returning the result to Partial view page. Partial view is one of the views that we can call inside Normal view page.
Partial View Result is returning the result to Partial view page. Partial view is one of the views that we can call inside Normal view page.
public PartialViewResult Index() {
return PartialView("_PartialView");
}
Redirect Result
Redirect result is returning the result to specific URL. It is rendered to the page by URL. If it gives wrong URL, it will show 404 page errors.
Redirect result is returning the result to specific URL. It is rendered to the page by URL. If it gives wrong URL, it will show 404 page errors.
public RedirectResult Index(){
return Redirect("Home/Contact");
}
Redirect to Action Result
Redirect to Action result is returning the result to a specified controller and action method. Controller name is optional in Redirect to Action method. If not mentioned, Controller name redirects to a mentioned action method in current Controller. Suppose action name is not available but mentioned in the current controller, then it will show 404 page error.
Redirect to Action result is returning the result to a specified controller and action method. Controller name is optional in Redirect to Action method. If not mentioned, Controller name redirects to a mentioned action method in current Controller. Suppose action name is not available but mentioned in the current controller, then it will show 404 page error.
public ActionResult Index () {
return RedirectToAction("Login", "Account");
}
Json Result
Json result is a significant Action Result in MVC. It will return simple text file format and key value pairs. If we call action method, using Ajax, it should return Json result.
Json result is a significant Action Result in MVC. It will return simple text file format and key value pairs. If we call action method, using Ajax, it should return Json result.
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);
}
File Result
File Result returns different file format view page when we implement file download concept in MVC using file result.
File Result returns different file format view page when we implement file download concept in MVC using file result.
public ActionResult Index () {
return File("Web.Config", "text");
}
Content Result
Content result returns different content's format to view. MVC returns different format using content return like HTML format, Java Script format and any other format.
Content result returns different content's format to view. MVC returns different format using content return like HTML format, Java Script format and any other format.
public ActionResult Contact(){
ViewBag.Message = "Your contact page.";
return View();
}
No comments:
Post a Comment