2

我正在尝试创建一个视图,它允许用户使用索引视图模型查看当前列出的项目,然后还允许用户使用单独的创建视图模型创建一个新项目

所以我有两个视图模型 -IndexFunkyThingsViewModel -CreateFunkyThingViewModel

本质上,我有一个主要观点:

@model IndexFunkyThingsViewModel
@foreach (var item in Model.FunkyThings)
{
/*Indexy stuff*/    
}

/*If create item*/
@if (Model.CreateFunkyThing)
{
@Html.Partial("_CreateFunkyThingPartial", new CreateFunkyThingViewModel());
}

然后在我的部分观点中,我有

@model CreateFunkyThingViewModel
@using (Html.BeginForm(MVC.FunkyThings.CreateFunkyThing(Model)))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Create FunkyThing</legend>
        @Html.EditorForModel();
        <p>
            <input type="submit" class="button green" value="CreateFunkyThing" />
        </p>
    </fieldset>
}

最后在控制器中我有:

[HttpPost]
public virtual ActionResult CreateFunkyThing(CreateFunkyThingViewModel createFunkyThingViewModel)
{
    ..
}

这一切似乎编译得很愉快,当我进入视图时,它可以显示创建字段等。然而,当我点击提交按钮时,控制器没有收到任何数据。ActionResult 被调用,但是在调试器中,createFunkyThingViewModel 参数在被提交按钮调用时为空。

我究竟做错了什么?

4

1 回答 1

2

发布到您的控制器时,您不会将模型发送给它。用这个:

 @using (Html.BeginForm("CreateFunkyThing", "ControllerName", FormMethod.Post))

然后从按钮周围删除 p 标签,不要使用任何东西。

段落标签倾向于将按钮与表单分开分组,即使它们位于同一个外部容器中。

于 2013-03-11T14:58:41.763 回答