Invoking ASP.NET MVC Action from within another controller action - asp.net-mvc

I have a controller action Foo. From within Foo I need to pass a GUID to another action called Index. how can I do that?
public ActionResult Foo() {
return View("Index",someguid);
}
public ActionResult Index(Guid id)
How can I do that?

Precisely what the RedirectToAction method is for.
As Dom mentioned in the comments, this should get you going in the right direction.
public ActionResult Foo() {
return RedirectToAction("Index", new { id = someguid });
}

Related

How to declare controller action method return type?

I understand ActionResult is the base class for RedirectResult class so essentially functionality for the code below is the same.
Is there an advantage to explicitly specify the appropriate result class as a return type as opposed to the base class?
public ActionResult Index()
{
return Redirect("Home/Contact");
}
public RedirectResult Index()
{
return Redirect("Home/Contact");
}
if you need redirect quickly to another action without lost any data and this action is in the same controller , call the action as a method:
public IActionResult Index()
{
return Contact();
}
public IActionResult Contact()
{
....
}
which one from another redirections methods is better is an opinion-based question and is not allowed by SO policy

Add 2 ActionResult DeleteConfirmed in 1 controller

Is it possible to have 2 ActionResult DeleteConfirmed in 1 controller?
I have 2 different views that I want to delete.
Thanks,
EB
[HttpPost, ActionName("DeleteLink")]
public ActionResult DeleteConfirmed(int id)
{
Link link = db.Links.Find(id);
db.Links.Remove(link);
db.SaveChanges();
return RedirectToAction("OutOfBank");
}
It seems that you want to overload DeleteConfirmed action. You can use the attribute if you want your code to do overloading.
[ActionName("MyOverloadedName")]
But, you'll have to use a different action name for the same http method.
So it's just semantics at that point. Would you rather have the name in your code or your attribute?
Code Example:
public class HomeController : Controller
{
public ActionResult GetEmpName()
{
return Content("This is the test Message");
}
[ActionName("GetEmpWithCode")]
public ActionResult GetEmpName(string EmpCode)
{
return Content("This is the test Messagewith Overloaded");
}
}
Phil has an article related to this: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx

Controller method overloading using attribute routing

I can change the method name but can anyone tell me why this doesn't work? How can i make it working without changing method name. Also I don't want to specify the action name in the view. Is it possible?
[HttpGet]
[Route("AddUpdateCategories/{storeId}")]
public ActionResult AddUpdateStoreCategories(int storeId)
{
return View();
}
[HttpPost]
public ActionResult AddUpdateStoreCategories(int StoreId,int[] ShopCategoryId)
{
return null;
}
Problem is post action is not getting called on submit.
You don't have to change the method name. The problem is that this post action has no route. If you use attribute routing, you have to specify a route for each action. Both of these actions would end up with the same route attribute, but the [HttpPost] attribute is enough for the routing framework to work out which one to use.
[Route("AddUpdateCategories/{storeId}")]
public ActionResult AddUpdateStoreCategories(int storeId)
{
return View();
}
[HttpPost]
[Route("AddUpdateCategories/{storeId}")]
public ActionResult AddUpdateStoreCategories(int StoreId,int[] ShopCategoryId)
{
return null;
}

MVC any action returns partial view of the same name

I have a controller where all of the action methods contain the same code:
[ActionName("pretty-url")]
public ActionResult Something() {
return PartialView();
}
[ActionName("another-pretty-url")]
public ActionResult SomethingElse() {
return PartialView();
}
I name my partial views in the pretty-url.cshtml format, and these get picked up fine and everything works.
As every action in the controller will always do exactly the same thing and return the same thing, I would like to just have my controller look for the correctly-named view and return it as above, without me having to explicitly implement it.
How would I do that?
TIA
I would create a single action and pass the view name as parameter.
public ActionResult Something(string viewName)
{
return PartialView(viewName);
}
I would add a new method to my controller with a string parameter and use it to load the correct partial view.
public ActionResult Show(string PartialName)
{
return PartialView(PartialName);
}
Now instead of using http://your.domain/pretty_url you will have to use http://your.domain/show/pretty_url but this will work with any new partial view you add later on.

Ambigious names for controller methods in ASP.NET MVC

Image the following controller method:
public ActionResult ShipmentDetails(Order order)
{
return View(new OrderViewModel { Order = order });
}
The incoming order parameter is filled from a custom model binder, that either creates a new order for this session and stores it in the session, or reuses an existing order from the current session. This order instace is now used to fill a shipment details form, where users can enter their address and so on.
When using #using(Html.BeginForm()) in the view. I cannot use the same signature for the post method again (because this would result in ambigious method names) and I found me adding a dummy parameter just to make this work.
[HttpPost]
public ActionResult ShipmentDetails(Order order, object dummy)
{
if (!ModelState.IsValid)
return RedirectToAction("ShipmentDetails");
return RedirectToAction("Initialize", order.PaymentProcessorTyped + "Checkout");
}
What are the best practices for this? Would you simply rename the method to something like PostShipmentDetails() and use one of the overloads of BeginForm? Or does the problem originate from the point, that the first method has the order parameter?
You could use the ActionName attribuite:
[HttpPost]
[ActionName("ShipmentDetails")]
public ActionResult UpdateShipmentDetails(Order order) { ... }
or a more classic pattern:
public ActionResult ShipmentDetails(int orderId)
{
var order = Repository.GetOrder(orderId);
return View(new OrderViewModel { Order = order });
}
[HttpPost]
public ActionResult ShipmentDetails(Order order)
{
if (!ModelState.IsValid)
return RedirectToAction("ShipmentDetails");
return RedirectToAction("Initialize", order.PaymentProcessorTyped + "Checkout");
}

Resources