MVC - 如何从视图传递模式控制器 [英] 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?
下面是我的code。
型号:
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 think 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)
问题是模型不返回我与填充它最初TBLRETURNABLEITEMS列表。它使其他属性我填充,但不在名单。如何应我想我保持这种模型的数据,而无需重新加载它的每一个控制器上。
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.
推荐答案
我认为你可以使用TempData的为。
I think that you can use TempData for that.
因此,像这样:
[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);
}
这篇关于MVC - 如何从视图传递模式控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!