MVC - How to pass model from view to controller
I am working with a model that needs to flow through a series of controllers and views manipulating it along the way(Only loading it on the first controller). Is there a way to persist the model from the view back down to a controller and so forth?
Here is my code.
Model:
public class ROWModel
{
#region Properties
//Request
public List<TBLRETURNABLEITEMS> TBLRETURNABLEITEMS { get; set; }
//public List<ReturnReasons> ReturnReasons { get; set; }
public int Order_No { get; set; }
public string First_Name {get; set; }
public string Last_Name {get; set; }
public string Company { get; set; }
public string Address_1 { get; set; }
public string Address_2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Postal_Code { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string CustomerCode {get; set; }
public string TerritoryCode {get; set; }
//Post
#endregion
#region Constructor
public ROWModel()
{ }
#endregion
}
public class ReturnableItems : IComparable<ReturnableItems>
{
private int _id;
private decimal _ordered;
private decimal _shipped;
public int Id
{
get { return _id; }
set { _id = value; }
}
public decimal Ordered
{
get { return _ordered; }
set { _ordered = value; }
}
public decimal Shipped
{
get { return _shipped; }
set { _shipped = value; }
}
}
After populating the model and sending it to the view everything is displayed using the model as it should be. I th开发者_开发百科ink stick the model like so on the form tag:
<% using (Html.BeginForm("Items", "ROW", Model))
Here is the post Items Action of the ROW controller:
[ActionName("Items"), AcceptVerbs(HttpVerbs.Post)]
public ActionResult Items(ROWModel model, FormCollection collection)
Problem is Model doesn't return the list of TBLRETURNABLEITEMS i populated it with initially. It keeps the other properties i populated but not the list. How do i maintain this model's data without having to reload it on every controller should i want to.
I think that you can use TempData for that.
So something like this:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult TestA()
{
MyModel model = new MyModel();
model.Something = "Test";
return View(model);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TestA(MyModel model)
{
TempData["MyModel"] = model;
return RedirectToAction("TestB");
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult TestB()
{
MyModel myModel = (MyModel)TempData["MyModel"];
return View(myModel);
}
NerdDinner Step 6: ViewData and ViewModel
http://nerddinnerbook.s3.amazonaws.com/Part6.htm
You can use session for this kind of problems. or you can use tempdata if the posts are sequential . and fetching each time from DB works if you run application in Intranet networks.
Getting a model (any model) from the view to the controller is pretty simple.
Others on here have covered that part where you take in a model object as a param.
what you seem to be missing is the binding of the list, which happens based on the names in your form elements in the view. basically,they have to look like C# list elements as strings. So, not knowing any of the properties of your list elements, something like:
<%= Html.TextBox("model.TBLRETURNABLEITEMS[0].ItemId") %>
Obviously,that's just a string, so you can construct it in a loop to do many rows of returnables.
I think in MVC as well session is a handy option instead of firefighting with the return to action with Viewmodel.
精彩评论