MVC3局部视图和模型不回发的变化 [英] MVC3 Partial View and Model not posting back changes

查看:80
本文介绍了MVC3局部视图和模型不回发的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比包含可修改的项目集合的模型。

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

我使用的局部视图,又使用EditorForModel输出HTML集合中的每个元素呈现集合。

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" />
    }

管窥

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

PlayerModel查看

PlayerModel View

@model Footy.Models.PlayerModel

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

有所有正确呈现,但是当用户点击输入​​,则控制器方法未在模型传递的子集,例如Model.Team1Players为null

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

我是什么失踪?

编辑:生成的HTML

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>

感谢

我认为这是关系到这个问题,它目前还没有一个答案:<一href=\"http://stackoverflow.com/questions/7457967/posting-data-back-to-a-controller-from-a-partial-view-rendered-by-ajax\">Posting数据传回从阿贾克斯呈现局部视图控制器

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

推荐答案

如果您检查所呈现源,你能确认的渲染子模型输入的名称和ID的对应模型层次?

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?

我相信你会需要 EditorFor 为了孩子的模型是正确的命名空间。

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

所以在EventModel视图中,使用这样的:

So in the EventModel view, use something like this:

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

我不知道但是。我有类似的问题与MVC框架。

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

这篇关于MVC3局部视图和模型不回发的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆