0

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

2
  • Please, can you edit the code part. Commented Apr 14, 2011 at 7:54
  • Please consider the line just above the gray box, as part of the code. Commented Apr 14, 2011 at 9:08

1 Answer 1

1

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)
        {
            ...
        }
    }
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Darin, I used this on the user control:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.