【问题标题】:Partial View submit returns null部分视图提交返回 null
【发布时间】:2013-03-11 14:42:48
【问题描述】:

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

所以我有两个 viewModel -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 参数在被提交按钮调用时为空。

我做错了什么?

【问题讨论】:

    标签: asp.net-mvc partial-views razor-2


    【解决方案1】:

    当发布到您的控制器时,您并没有将模型发送给它。使用这个:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多