参数名称必须是模型吗? [英] Does the name of parameter have to be model?

查看:18
本文介绍了参数名称必须是模型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到一个奇怪的问题,我的模型没有绑定并在控制器上显示为空.

我有一个做 httppost 的表单.我在控制器中的断点被击中,我希望成为我的模型的参数为空.

查看另一个工作页面上的一些示例代码,我复制并粘贴了它,唯一的区别是参数的名称是模型"而不是消息.

查看

@model Site.Models.ContactMessage@{ViewBag.Title = "索引";}<h2>索引</h2>@using (Html.BeginForm()){@Html.AntiForgeryToken()<div class="form-horizo​​ntal"><h4>ContactMessage</h4><小时/>@Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="form-group">@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })

<div class="form-group">@Html.LabelFor(model => model.To, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.EditorFor(model => model.To, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.To, "", new { @class = "text-danger" })

<div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Save" class="btn btn-default"/>

}<div>@Html.ActionLink("返回列表", "索引")

控制器

 public ActionResult Contact(){返回视图();}[HttpPost]公共 ActionResult 联系人(ContactMessage 消息){var m = 消息;返回视图();}

它奏效了.我想我一定完全错过了一些关于命名约定的内容.发现你可以使用绑定,从阅读一堆其他帖子中,更改前缀;

 public ActionResult Contact([Bind(Prefix = "model")] ContactMessage message)

但这没有用,仍然为空.打算将它重命名为模型,这样它就可以工作了,我可以继续前进,但想知道如果不调用模型,它为什么不具有约束力.

 public ActionResult Contact(ContactMessage 消息)

改回如上,但仍返回空值.有趣的是,如果我打开另一个 MVC 应用程序,该应用程序具有我想要的任何参数名称并且可以正常工作.它使用的是旧版本的 MVC 5(尚未更新,但我会这样做,看看是否会发生任何事情.我不希望它会发生.)

解决方案

您的问题是您的模型包含一个名为 Message 的属性,并且您还有一个名为 message 的参数DefaultModelBinder 读取包含 message = "someTextValue" 的表单值并搜索名称为 message 的模型属性.它在你的模型中找到一个并设置它的值(到目前为止一切正常)但然后它找到另一个(你的参数)并尝试设置一个复杂对象的值 string 值(实际上 stringcode>ContactMessage message = "someTextValue";) 失败,因此模型变为 null

Hit a strange issue where my model is not binding and shows up on the controller as null.

I have a form doing a httppost. My breakpoint in the controller is hit and the parameter I expect to be my model is null.

Looking at some example code on another page that works, I copied and pasted it and the only difference was the name of the parameter was 'model' instead of message.

View

@model Site.Models.ContactMessage

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>ContactMessage</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.To, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.To, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.To, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Controller

    public ActionResult Contact()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Contact(ContactMessage message)
    {
        var m = message;
        return View();
    }

and it worked. I thought I must have entirely missed something about naming convention. Found you can use Bind, from reading a heap of other posts, to change the prefix like;

    public ActionResult Contact([Bind(Prefix = "model")] ContactMessage message)

but that didn't work, still null. Going to rename it to model so it works and I can move on but would like to know why it's not binding if not called model.

    public ActionResult Contact(ContactMessage message)

Changed back to this as above but still returns a null. Interestingly, if I open up another MVC app, that one has whatever parameter names I want and works fine. It's using an older version of MVC 5 (not updated it yet but I will do that and see if anything happens. I don't expect it will.)

解决方案

Your problem is that you model contains a property named Message and you also have a parameter named message The DefaultModelBinder reads the form values which will include message = "someTextValue" and searches for model properties that have the name message. It finds the one in you model and sets it value (all OK so far) but then it finds another one (your parameter) and tries to set the value of a complex object string value (in effect ContactMessage message = "someTextValue";) which fails so the model becomes null

这篇关于参数名称必须是模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆