ASP.NET MVC Finding the calling method when! ModelState.IsValid

I have an action method on my "CartController" AddtoCart that returns an ActionResult. The problem I am having is that I am sending the identifier of the product I want to add from the antoher controller to AddtoCart, and then go. I have no problem checking; however, when I want to redirect to the view that triggered the action, when! ModelState.IsValid, I do not know who called me (or where to find him).

It is possible that several different controllers may send a message to the method. Is there something in ViewData that I can use to find out who called my action method?

+4
source share
3 answers

It sounds like you:

Request.UrlReferrer 

Let me know if you do not.

HTHS,
Charles

+4
source

I do not think controllers are making a message. The controllers receive requests (messages) and do some work, extract the data, and then choose which view to display back to the browser.

So, your action methods are usually called from a web browser (link on the page, javascript). That's why I suggest you pass an additional parameter to the action methods, and then based on this value, select the appropriate view for rendering.

 public ActionResult AddToCart(int productID, string caller) { //add to cart logic switch (caller) { case "this": { //get data for this view return View("this"); } case "that": { //get data for that view return View("that"); } default: { //get data for default view return View("default"); } } } 

I hope that I understand well what the nature of your problem is ...

0
source

I think you are after something like this:

[...] if you don't mind that your code is tied to a specific view engine that you use, you can look at the ViewContext.View property and pass it to WebFormView

 var viewPath = ((WebFormView)ViewContext.View).ViewPath; 

from a related question about getting a view name from inside a controller method.

0
source

All Articles