开发者

Form input in a foreach loop returns empty model

开发者 https://www.devze.com 2023-02-24 07:53 出处:网络
I have a list object for which I tried to display text boxes in a foreach loop. However the post returns empty object. I couldn\'t see the cause.

I have a list object for which I tried to display text boxes in a foreach loop. However the post returns empty object. I couldn't see the cause.

Here is the code in the view

<%using (Html.BeginForm("makeTransfer", "shareTransfer")) { %>

        <% foreach (var i in Model.Inform)//int i = 0; i < Model.Inform.Count(); i++){ %>
                <%:Html.HiddenFor(x=>i.shares, new{@value = i.shares}) %> 
              ...
                <td style = "width:20px"><%:Html.TextBoxFor(x=>i.sharesRq)%></td> cuddling
        <%} %>

    <%:Html.HiddenFor(x => x.accSrc, new { @value = Model.accSrc })%>
            <%:Html.HiddenFor(x=>x.accDst, new{ @value = Model.accDst}) %>

    Date of Transfer&l开发者_如何学Pythont;%:Html.TextBoxFor(x => x.date)%>
            Transfer with benefit<%:Html.CheckBoxFor(x => x.withBenefit)%>

       <input type="submit" name="save" value="Save" /></div>
       <input type="submit" name="cancel" value="Cancel" /></div>  

<%} %>

And Here is the controller

public ActionResult makeTransfer(vmTransfer transfer, string save, string cancel) {

        if (cancel != null)
            return RedirectToAction("startTransfer");

        else if (save != null)
        {

            foreach (var t in transfer.Inform)
            { ...

My problem is, transfer.Inform( 2nd line from the last) which is a list is empty when the form posts. Any help please, ASAP.


I would recommend you using editor templates instead of writing any loops in your views:

<% using (Html.BeginForm("makeTransfer", "shareTransfer")) { %>
    <%= Html.EditorFor(x => x.Inform) %>
    <%= Html.HiddenFor(x => x.accSrc, new { @value = Model.accSrc }) %>
    <%= Html.HiddenFor(x => x.accDst, new { @value = Model.accDst }) %>
    Date of Transfer <%= Html.TextBoxFor(x => x.date) %>
    Transfer with benefit <%= Html.CheckBoxFor(x => x.withBenefit) %>

    <input type="submit" name="save" value="Save" /></div>
    <input type="submit" name="cancel" value="Cancel" /></div>  
<% } %>

and in the corresponding editor template (~/Views/Shared/EditorTemplates/InformViewModel.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.InformViewModel>" 
%>
<%= Html.HiddenFor(x => x.shares) %> 
...
<td style="width:20px">
    <%= Html.TextBoxFor(x => x.sharesRq) %>
</td>

Remark: you might need to adjust the name of the editor template based on the type of the Inform property.

Editor templates will take care of generating proper id and names of the input fields so that everything binds correctly:

[HttpPost]
public ActionResult makeTransfer(vmTransfer transfer, string save, string cancel)
{
    if (cancel != null)
    {
        return RedirectToAction("startTransfer");
    }
    else if (save != null)
    {
        foreach (var t in transfer.Inform)
        {
            ...
        }
    }
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号