简体   繁体   English

MVC剃须刀张贴模型为空

[英]MVC razor post model is null

Probably missing something obvious here, but my model posted to the controller is always null... The model passes to the view correctly and I can access the properties, but when submitting the form, the model is null. 可能这里缺少明显的东西,但是我发布到控制器的模型始终为空...模型已正确传递到视图,并且我可以访问属性,但是在提交表单时,模型为空。 Why? 为什么?

Model... 模型...

public class ProfileModel
{

    public MembershipUser user { get; set; }
    public ProfileSettingsModel settings { get; set; }


}

Controller... 控制器...

    [HttpPost]
    public ActionResult SaveSettings(ProfileModel model)
    {
        var x = model.user.UserName;
        return View();
    }

View... 视图...

 @model MyApp.Models.Profile.ProfileModel

 @using(Html.BeginForm("SaveSettings","Profile"))
    {

        //Tried removing these as I shouldn't need them, but no luck.
        @Html.HiddenFor(x=>x.settings)
        @Html.HiddenFor(x=>x.user)

        @Html.CheckBoxFor(model=>model.settings.VisitorsCanAddMeAsFriend)
        @Html.LabelFor(x=>x.settings.VisitorsCanAddMeAsFriend, "Visitors can add me as a friend")
        @Html.CheckBoxFor(x=>x.settings.VisitorsCanMessageMe)
        @Html.LabelFor(x=>x.settings.VisitorsCanMessageMe, "Visitors can message me")

         <button id="save" type="submit" class="btn btn-default">Save</button>

     }

Rendered HTML... 呈现的HTML ...

<form action="/Profile/SaveSettings?HttpMethod=POST&amp;InsertionMode=Replace&amp;LoadingElementDuration=0&amp;AllowCache=False" method="post" novalidate="novalidate">
    <input data-val="true" data-val-required="The VisitorsCanAddMeAsFriend field is required." id="settings_VisitorsCanAddMeAsFriend" name="settings.VisitorsCanAddMeAsFriend" type="checkbox" value="true">
    <input name="settings.VisitorsCanAddMeAsFriend" type="hidden" value="false">
    <label for="settings_VisitorsCanAddMeAsFriend">Visitors can add me as a friend</label>
    <input data-val="true" data-val-required="The VisitorsCanMessageMe field is required." id="settings_VisitorsCanMessageMe" name="settings.VisitorsCanMessageMe" type="checkbox" value="true">
    <input name="settings.VisitorsCanMessageMe" type="hidden" value="false">
    <label for="settings_VisitorsCanMessageMe">Visitors can message me</label>                 
    <button id="save" type="submit" class="btn btn-default">Save</button>
</form>

Is there anything obvious? 有什么明显的吗?

Change 更改

@Html.HiddenFor(x=>x.settings)
@Html.HiddenFor(x=>x.user)

To

@Html.HiddenFor(x => x.user.UserName)

The default binder should at least pick that one up. 默认的活页夹至少应选择一个。

You cannot use MembershipUser in model binding. 您不能在模型绑定中使用MembershipUser。 Model binding requires that all properties have a public parameterless constructor, and MembershipUser's parameterless constructor is protected. 模型绑定要求所有属性都具有公共的无参数构造函数,并且MembershipUser的无参数构造函数受到保护。 As such, you cannot use it directly. 因此,您不能直接使用它。 You will need to create your own view model to pass the properties. 您将需要创建自己的视图模型以传递属性。

So, removing the settings and user HiddenFor's, then biding those properties directly will get you what you want. 因此,删除设置和用户HiddenFor的设置,然后直接出价这些属性将获得所需的内容。

I didn't think you could do a html.hiddenfor (complex type), as the value has to represented as a string. 我认为您无法执行html.hiddenfor(复杂类型),因为该值必须表示为字符串。 In your render html you can see that then two HiddenFor's are not rendered as input type='hidden' ...../ 在渲染html中,您可以看到两个HiddenFor都没有渲染为输入type ='hidden'..... /

So yes, the obvious error is the two html.hiddenfor. 因此,是的,明显的错误是两个html.hiddenfor。 You will need to do a HiddenFor or each property that you wish to receive in the post 您需要做一个HiddenFor或您希望在帖子中收到的每个属性

In the controller's postback method, the last line says 在控制器的回发方法中,最后一行说

return View();

but it should say 但应该说

return View(model);

otherwise you are not passing the model back to the page. 否则,您不会将模型传递回页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM