Do I need a presentation for every action?

I have been developing in ASP.NET MVC for a short time. Up to this point, the actions of the controller were quite simple. Each action returns a corresponding view. However, I recently created an action for which I do not need a page (the purpose of the action is to work with the database).

My question is what to do here. At the end of the method, I return Response.Redirect ('\ Controller \ View'), so I return to another view. Is it possible not to return any view at the end of the action? What are the best practices here?

+4
source share
4 answers

If you need to redirect the user because he clicked the link, redirect the user.

If your publication is using Ajax or another method and there is no meaningful answer, change the controller action method to the return type of void.

+2
source

If you are sending messages to AJAX and you do not need to redirect the user or display a new view, you may need to return a line that displays the confirmation to the user.

+1
source

I would say that an action should always process an HTTP request. If it returns a view or redirects to another action, both are possible.

Consider the following:

[HttpGet] // Handles only GET requests public ActionResult Edit(int id) { // get entity from repository // and create edit model return View(editModel); } [HttpPost] public ActionResult Edit(EntityEditModel editModel) { // if ModelState is valid, save entity // and if success redirect to index return RedirectToAction("Index"); } 

The first action returns the view, the second does not (only if the ModelState is invalid, it displays the "Edit" view again). And this is absolutely right to do (this is even recommended). But both actions process the HTTP request.

0
source

Tilde syntax can be used to provide a complete view path, as shown below:

Indication of view

 public ActionResult Index() { ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; return View("~/Views/Example/Index.cshtml"); } 

When using tilde syntax, you must provide a view file extension because it bypasses the lookup of internal search engines to search for views.

0
source

All Articles