Model passed to a partial view is null upon form submit

我们两清 提交于 2019-12-11 08:28:19

问题


I have the following model:

@model SmartSEOModel

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

    public SEOTemplateModel SEOTemplateModel { get; set; }
}

In my view I have a partial view which I call like this:

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

    <div id="pnlSmartSEO">

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

    </div>
}

In the partial view there are some form fields bound to the SEOTemplateModel. The problem is that when I receive the SmartSEOModel in my HttpPost action, the SEOTemplateModel is null. As if the SEOTemplateModel has been passed by copying it to the partial view.

Please advise why this is and how to workaround it.

Many thanks

My partial view looks like this:

@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>
))
}

My HttpPost action looks like this:

[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);
    }

回答1:


Becuase you don't have a form within your partial view, it will not persist the data. Try using @Html.EditorFor instead of @Html.Partial.

So your main view would look like

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

    <div id="pnlSmartSEO">

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

    </div>
}

You would then need to move your partial view into a template. Rename your partial view to EditorTemplates\SEOTemplateModel.cshtml and place it in the same location where your main view is.

You will also need to make your template strongly typed: @model [namespace].SEOTemplateModel



来源:https://stackoverflow.com/questions/8490679/model-passed-to-a-partial-view-is-null-upon-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!