1

I have got a problem with submitting data to Post method.

I have a class similar to this:

public class A
{
  public int Id{get; set;}
  public string Name{get; set;}
  public List<B> Bs {get; set;}
}

In the View I have a list of Bs object that I can add to the Bs property of class A without any problem. I can see the result without any problem in my Razor View as well.

The problem occurs when I post the final result to my Add Action Method to save them in my database.

When the final object is sent to my Action method the Bs property is completely empty. So what I did instead was to add that collection to a TempData dictionary and retrieved it in my Add Action method.

My question is simple why is my Bs property empty when its posted to my Edit action method ? The reason why I ask this is because in my AddBs action method I add my Bs to my Model and send it back to View and everything is find up to that point.

Edit: @SLaks

This is the code I have in my View :

<% using (Html.BeginForm("Create", "A")) { %> <%: Html.ValidationSummary(true) %>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Name) %>
            </div>
            <div class="editor-field">
                <%: Html.EditorFor(model => model.Name) %>
                <%: Html.ValidationMessageFor(model => model.Name) %>
            </div>

            <% if (Model.Bs.Any())
               { %>
                    <h3>B List</h3>
                    <%= Html.Telerik().Grid(Model.Bs)
                    .Name("CheckedOrders")
                    .Columns(columns =>
                    {
                        columns.Bound(d => d.Id).Width(100);
                        columns.Bound(d => d.Name).Width(300);
                    })
                    .Footer(false)
                    %>
             <% } %>

            <br />

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    <% } %>

This is the HTML generated:

<form action="/Admin/BusinessType/Create" method="post">
    <fieldset>

        <div class="editor-label">
            <label for="Name">Name</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" id="Name" name="Name" type="text" value="" />

            <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>

        </div>                 
                <div class="t-widget t-grid" id="CheckedOrders"><table cellspacing="0"><colgroup><col style="width:100px" /><col style="width:300px" /></colgroup><thead class="t-grid-header"><tr><th class="t-header" scope="col">Id</th><th class="t-last-header t-header" scope="col">Name</th></tr></thead><tbody><tr><td>2</td><td class="t-last">Heeey</td></tr><tr class="t-alt"><td>3</td><td class="t-last">Testtttt</td></tr></tbody></table></div>

        <br />
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
</form>

The Code above shows perfectly fine in my Browser, I can even see the Bs list, but when I type a Name And Click on Create, the Bs list is empty.

4
  • What names does it give to the controls Grid produces? Commented Feb 21, 2011 at 21:21
  • @Rob I have added the generated HTML to the question. Commented Feb 21, 2011 at 22:17
  • 1
    Looking at the HTML there doesn't seem to be any form controls generated for the Bs elements (so none can get posted back). You can always tell if there is a chance of the values being used by looking at Request.Form and Request.QueryString in your action method. Commented Feb 22, 2011 at 0:05
  • Would it be possible to add the collection to a hidden form may be ? Commented Feb 22, 2011 at 10:20

2 Answers 2

1

I think the secret to this is in the view. I have a similar model in my app. In the view you have to format the names of the controls correctly:

<%
for (int i=0; i<model.Bs.Count; i++)
{
  Html.TextBox( string.Format( "Bs[{0}].Property1OfB", i), model.Bs.Property1OfB );
  Html.TextBox( string.Format( "Bs[{0}].Property2OfB", i), model.Bs.Property2OfB );
}
%>
Sign up to request clarification or add additional context in comments.

2 Comments

that is not very pretty, I am using Telerik helper method, so I was expecting Telerik to handle the naming peroperly.
Pretty it certainly isn't (but it does work for me). Also remember that the Bs class needs to have a get/set on its properties.
0

The Add method will only receive whatever <input> elements are in the form that posted to it.

3 Comments

The List of Bs that are displayed in View are all within the <input> elements ! However I still get empty Bs.
One more thing, I used Telerik Grid between my input tags : demos.telerik.com/aspnet-mvc would this cause any issues maybe ?
Ok I will show you my view once I get home. I am at work right now.

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.