请问参数的名称必须是模式? [英] Does the name of parameter have to be model?

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

问题描述

打一个奇怪的问题,在我的模式是不具有约束力,并显示了控制器上的空

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

我有一个表格做一个httppost。我在控制器中断点时,我希望是我的模型参数为空。

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.

看着这样的作品,我复制并粘贴它,唯一的区别是参数的名字,而不是消息模式的另一页上的一些例如code。

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.

查看

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

控制器

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

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

和它的工作。我想我一定是完全错过了一些关于命名约定。发现可以使用绑定,从阅读其他职位一堆,改变preFIX等;

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)

改回这个如上,但仍返回null。
有趣的是,如果我打开了另一MVC应用程序,一个有我想要的任何参数名称和工作正常。它使用MVC 5的旧版本(没有更新过,但是我会做到这一点,看看有什么事情发生,我不希望它会的。)

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.)

推荐答案

您的问题是,你的模型包含名为属性消息,你也有一个命名参数的消息 DefaultModelBinder 读取表单值,其中将包括消息=someTextValue并具有名模特属性搜索的消息。它发现一个在你的模型,并将其设置值(一切OK为止),但是它找到另一个(你的参数),并试图设置一个复杂的对象字符串值(实际上 ContactMessage消息=someTextValue; )未有因此模型变得

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天全站免登陆