开发者

MVC3 Partial View and Model not posting back changes

I have a Model than contains a collection of items that can be modified.

I render the collection using a Partial View, which in turn uses EditorForModel to output the HTML for each element in the collection.

@model Footy.Models.EventModel

    <h2>@Model.Team1Name vs @Model.Team2Name</h2>
    @using (Html.BeginForm("Index", "Event"))
    {
        @Html.HiddenFor(m => m.EventID)
        <h1>
            Team 1 Squad</h1>
        @Html.Partial("EventPlayers", Model.Team1Players);
        <h1>
            Team 2 Squad</h1>
   Html.RenderPartial("EventPlayers", Model.Team2Players);

        <input type="submit" value="Confirm Changes" />
    }

Partial View

@model IEnumerable<Footy.Models.PlayerModel>
@Html.EditorForModel()

PlayerModel View

@model Footy.Models.PlayerModel

@Model.PlayerName @Html.DropDownListFor(p => p.ParticipationStatusID, new SelectList(Model.ParticipationTypes, "Key", "Value"))

It all renders correctly, but when the user clicks the input, the controller method is not passed the child collection in the model, e.g. Model.Team1Players is null

What am I missing?

EDIT: Generated HTML is

    <form action="/Footy/Event/Index/1" method="post"><input data-val="true" data-val-number="The field EventID must be a number." data-val-required="The EventID field is required." id="EventID" name="EventID" type="hidden" value="1" />            
<h1>Team 1 Squad</h1>
      si <select data-val="true" data-val-number="The field ParticipationStatusID must be a number." data-val-required="The ParticipationStatusID field开发者_如何学编程 is required." name="[0].ParticipationStatusID"><option value="1">Team</option>
    <option value="2">Sub</option>
    <option value="3">Squad</option>
    </select>
    <h1>Team 2 Squad</h1>
 <input type="submit" value="Confirm Changes" />
 </form>

Thanks

I think it is related to this question, which doesn't yet have an answer: Posting data back to a controller from a Partial View rendered by Ajax


If you inspect the rendered source, can you check that the names and id's of the rendered child model inputs correspond to the model hierarchy?

I believe that you will need EditorFor in order for the child models to be properly "namespaced".

So in the EventModel view, use something like this:

@Html.EditorFor(m => m.Team1Players, "EventPlayers")

I am not sure however. I've had similar problems with the MVC framework.


Maybe the problem is in the collection binding.

Try reading this post. It should give you some insight on collection binding

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜