简体   繁体   中英

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

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

You can also try another example here: 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. It passes the html attributes to the TextBoxFor method:

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

Request.Form["yourName"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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