Is there a way to pass an IEnumerable parameter to another method - asp.net-mvc

I am trying to pass a IEnumerable model parameter from one method to another however when I debug the code the parameter is not being passed even though the method signature is the same.
I've tried with and without the Bind Prefix
Method 1:
[HttpPost]
public ActionResult Create_Filter([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<CourseFilterViewModel> courseFilterVM)
{
return RedirectToAction("Course", "Filter", courseFilterVM);
}
Method 2:
public ActionResult Course([Bind(Prefix = "models")]IEnumerable<CourseFilterViewModel> courseFilterVM)
{
return View(courseFilterVM);
}
I am expecting the entire model to be passed but instead I am getting a null value.

You can simply do as follows:
[HttpPost]
public ActionResult Create_Filter([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<CourseFilterViewModel> courseFilterVM)
{
// Others stuffs here
return Course(courseFilterVM);
}

If you want to pass a model to the View
public ActionResult Create_Filter()
{
return View();
OR
return View(CourseFilterVM); // Pass as a parameter
}
VIEW - Create_Filter.cshtml
#model IEnumerable<CourseFilterViewModel>

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

Invoking ASP.NET MVC Action from within another controller action

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 });
}

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;
}

Generic object/class type to pass back to httppost?

I have a method that passes back my viewmodel from a view as such in my post:
[HttpPost]
public ActionResult DoStuff(daViewModel model)
{
string whatever = model.Name;
int id = model.Id;
return View();
}
What type of object get's passed back to my controller method on the post (is my viewmodel wrapped in an httppost type of class?) Is there a generic/type that I can pass such as:
[HttpPost]
public ActionResult DownloadFiles(object model)
{
// cast my daViewModel from object model as passed in???
string whatever = model.Name;
int id = model.Id;
return View();
}
You can pass a FormCollection object:
[HttpPost]
public ActionResult DownloadFiles(FormCollection collection)
{
// if you want to extract properties directly:
string whatever = collection["Name"];
int id = int.Parse(collection["Id"]);
// if you want to convert the collection to your model:
SomeModel model;
TryUpdateModel(model, collection);
return View();
}
The TryUpdateModel method returns a boolean. If it succeeds in updating the model it will return true, otherwise it will return false. The form values being passed in should match the property names of your model.
If you're asking what model gets passed back when you call return View() then the answer to that is nothing unless you tell it to. There is an overload on the View() method which takes in a model:
return View(model);
You should be returning the type that the View expects to see. If you defined your view as having a model of Foo then you better return a Foo in your controller.

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