简体   繁体   English

ASP.NET MVC数据不会从使用的部分视图传到控制器

[英]ASP.NET MVC The data doesnt come to controller from partial view used

I have main view and main model. 我有主视图和主模型。

Inside this view i have such lines 在此视图内,我有这样的线条

 foreach (var loadTask in Model.LoadTasks)
 {
     Html.RenderPartial("TripUpdateTask", new TripUpdateTaskModel { Task = loadTask });
 }

So in main model i have the 所以在主模型中

  public List<OrderTaskRecord> LoadTasks { get; set; }

Submodel is: 子模型是:

public class TripUpdateTaskModel
{
    public OrderTaskRecord Task { get; set; }
}

I played and played but still unable to save the data. 我玩了,但仍然无法保存数据。 Here is current and simple look. 这是当前的简单外观。

<tr>    
    <td>Actual Time:</td>
    <td>@Html.TextBoxFor(m => m.Task.Task.ActualTime)</td>
</tr>

In raw html these time controls have same name and same id. 在原始html中,这些时间控件具有相同的名称和ID。 So i dont know what need to do to save that. 所以我不知道该怎么做才能节省下来。

I mean the data entered on main view back to controller fine, but not from partial view 我的意思是在主视图上输入的数据可以返回到控制器,但不是从局部视图

I use Html.BeginForm and fieldset in main view like this: 我在这样的主视图中使用Html.BeginForm和fieldset:

 @using (Html.BeginForm("TripUpdate", "SupplierBookingUpdate", FormMethod.Post, new { id = "SupplierBookingUpdateSave" }))
 {
    <fieldset>
    .. table
    </fieldset>
 }

Rather than trying to name each text box individually, I would recommend to bind your collection of OrderTaskRecord . 我建议不要绑定您的OrderTaskRecord集合,而不是尝试分别命名每个文本框。 That way you do not need to worry about a number of text boxes and their names. 这样,您无需担心许多文本框及其名称。

There's a good introduction from Scott Hanselman on the subject. Scott Hanselman对该主题做了很好的介绍。

You can also try another example here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ 您也可以在这里尝试另一个示例: http : //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

If you want to pass a custom name to the input being generated use this. 如果要将自定义名称传递给正在生成的input ,请使用此名称。 It passes the html attributes to the TextBoxFor method: 它将html属性传递给TextBoxFor方法:

@Html.TextBoxFor(m => m.Task.Task.ActualTime, new { Name = "yourName" )

Of course, you can create counters, etc in here to keep them unique. 当然,您可以在此处创建计数器等,以使其保持唯一。

You can extract them later in your Controller by getting the Form data: 您可以稍后通过获取Form数据在Controller中提取它们:

Request.Form["yourName"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM