asp.net-mvc MVC - 如何将模型从视图传递到控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1842161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:58:24  来源:igfitidea点击:

MVC - How to pass model from view to controller

asp.net-mvcmodel-view-controller

提问by Billy Logan

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 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:

这是 ROW 控制器的 post Items Action:

    [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.

问题是模型没有返回我最初填充它的 TBLRETURNABLEITEMS 列表。它保留我填充的其他属性,但不保留列表。我如何维护这个模型的数据,而不必在我想要的每个控制器上重新加载它。

回答by rrejc

I think that you can use TempData for that.

我认为您可以为此使用 TempData。

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

回答by Robert Harvey

NerdDinner Step 6: ViewData and ViewModel
http://nerddinnerbook.s3.amazonaws.com/Part6.htm

NerdDinner 第 6 步:ViewData 和 ViewModel
http://nerddinnerbook.s3.amazonaws.com/Part6.htm

回答by ali62b

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.

您可以使用 session 来解决此类问题。或者,如果帖子是连续的,您可以使用 tempdata。如果您在 Intranet 网络中运行应用程序,并且每次从 DB 获取都有效。

回答by Ajith Menon

I think in MVC as well session is a handy option instead of firefighting with the return to action with Viewmodel.

我认为在 MVC 中会话也是一个方便的选择,而不是通过 Viewmodel 返回操作来进行灭火。

回答by Paul

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:

您似乎缺少的是列表的绑定,这是根据视图中表单元素中的名称发生的。基本上,它们必须看起来像 C# 列表元素作为字符串。因此,不知道列表元素的任何属性,例如:

<%= 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.

显然,这只是一个字符串,因此您可以在循环中构造它来执行多行可返回值。