【问题标题】:Model passed to a partial view is null upon form submit表单提交时传递给局部视图的模型为空
【发布时间】:2014-02-20 00:27:59
【问题描述】:

我有以下型号:

@model SmartSEOModel

public class SmartSEOModel
{
    public SmartSEOSettingsModel SmartSEOSettingsModel { get; set; }

    public SEOTemplateModel SEOTemplateModel { get; set; }
}

在我看来,我有一个局部视图,我这样称呼它:

@using (Html.BeginForm())
{
    some razor code here

    <div id="pnlSmartSEO">

        @Html.Partial(ViewNames.SmartSEOController_SEOTemplate, Model.SEOTemplateModel)

    </div>
}

在局部视图中有一些表单字段绑定到 SEOTemplateModel。 问题是当我在 HttpPost 操作中收到 SmartSEOModel 时,SEOTemplateModel 为空。就好像 SEOTemplateModel 已经通过将其复制到局部视图来传递一样。

请告知这是为什么以及如何解决它。

非常感谢

我的局部视图如下所示:

@Html.Telerik().TabStrip().Name("SmartSEO").Items(x =>
{
    x.Add().Text(T("Admin.SmartSEO").Text).Content(GetSmartSEOUI().ToHtmlString()).Selected(true);
})

@helper GetSmartSEOUI()
{
@(Html.LocalizedEditor<SEOTemplateModel, SEOTemplateLocalizedModel>("SmartSEO-Localized",
    @<table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Locales[item].CategoryTitleSEOTemplate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Locales[item].CategoryTitleSEOTemplate)
            </td>
        </tr>
    </table>,
    @<table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CategoryTitleSEOTemplate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CategoryTitleSEOTemplate)
            </td>
        </tr>
    </table>
))
}

我的 HttpPost 操作如下所示:

[HttpPost]
    public ActionResult Configure(SmartSEOModel smartSEOModel)
    {
        var seoTemplate = SEOTemplateService.GetSEOTemplateById(smartSEOModel.SEOTemplateModel.Id);

        if(seoTemplate == null)
        {
            throw new ArgumentException(String.Format("No SEOTemplate found with Id {0}", smartSEOModel.SEOTemplateModel.Id));
        }

        if (!ModelState.IsValid)
        {
            RedirectToAction("Configure");
        }

        SettingService.SaveSetting(smartSEOModel.SmartSEOSettingsModel.ToEntity());
        seoTemplate = smartSEOModel.SEOTemplateModel.ToEntity(seoTemplate);
        SEOTemplateService.UpdateSEOTemplate(seoTemplate);
        UpdateLocales(seoTemplate, smartSEOModel.SEOTemplateModel);

        //activity log
        CustomerActivityService.InsertActivity("EditSEOTemplate", LocalizationService.GetResource("ActivityLog.EditSEOTemplate"));

        SuccessNotification(LocalizationService.GetResource("SevenSpikes.NopSmartSEO.Admin.SEOTemplate.Notifications.SEOTemplateEdited"));

        return View("SevenSpikes.Nop.Plugins.SmartSEO.Views.Configure", smartSEOModel);
    }

【问题讨论】:

  • 你也可以发布部分html吗?
  • 局部视图中有表单吗?您是否在该视图中使用 jQueryUI 对话框?
  • 您能发布表单部分视图和发布操作代码吗?
  • 在这一行: var seoTemplate = SEOTemplateService.GetSEOTemplateById(smartSEOModel.SEOTemplateModel.Id); smartSEOModel.SEOTemplateModel 为空。
  • 请注意,如果它在主视图中而不是在局部视图中,这将起作用。我不能在主视图中使用它,但是由于与这篇文章相关的技术原因

标签: asp.net-mvc asp.net-mvc-3 razor partial-views model-binding


【解决方案1】:

因为您的局部视图中没有表单,所以它不会保留数据。尝试使用@Html.EditorFor 而不是@Html.Partial

所以你的主视图看起来像

@using (Html.BeginForm())
{
    some razor code here

    <div id="pnlSmartSEO">

        @Html.EditorFor(model => model.SEOTemplateModel)

    </div>
}

然后您需要将局部视图移动到模板中。将局部视图重命名为 EditorTemplates\SEOTemplateModel.cshtml 并将其放置在主视图所在的相同位置。

您还需要将模板设为强类型:@model [namespace].SEOTemplateModel

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 2017-05-12
    • 2015-12-03
    • 1970-01-01
    • 2014-02-05
    相关资源
    最近更新 更多