简体   繁体   English

MVC 4模型绑定返回null

[英]MVC 4 Model Binding returns null

I'm having trouble with model binding in MVC. 我在MVC中无法进行模型绑定。 I have a class: 我有一堂课:

public class UserSurvey
{
    public int Id { get; set; }
    public virtual Survey Survey { get; set; }
}

Which is the model for a view: 视图的模型是:

@model SurveyR.Model.UserSurvey

<form id="surveyForm">
    <div class="container survey">
        @Html.HiddenFor(x=>x.Id)

        @Html.EditorFor(x => x.Survey.Steps)
    </div>

    <input type="button" value="Submit" id="btnSubmit"/>
</form>

And then for the submit the controller takes a class: 然后对于提交,控制器采用一个类:

public class SurveyResponseViewModel
{
    public int Id { get; set; }
    public Survey Survey { get; set; }
}

[HttpPost]
public ActionResult Submit(SurveyResponseViewModel surveyResponse)
{
    ...
}

When I debug the submit the surveyResponse.Survey object is populated as it should be but the surveyResponse.Id value is 0 when it should be 1. 当我调试提交时,按原样填充了SurveyResponse.Survey对象,但是当应将SurveyResponse.Id值设置为1时,该对象为0。

I can see the Id=1 being passed back in the submit but the model binding doesn't seem to hook it up. 我可以看到Id = 1在提交中被传递回来,但是模型绑定似乎并没有将它挂钩。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Kev 千电子伏

EDIT: The rendered html looks like this: 编辑:呈现的html看起来像这样:

<form id="surveyForm">
    <div class="container survey">
        <input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="1" />

So yes the value appears there and is also passed in the submit if I look using dev tools. 因此,是的,如果我使用开发工具,该值将显示在此处,并且还会在提交中传递该值。

EDIT 2: The Form data in dev tools definitely contains "Id:1". 编辑2:在开发工具中的窗体数据肯定包含“ Id:1”。

Your Code seems to be fine.Try passing the id value explicitly as another parameter like below 您的代码似乎很好。尝试将id值显式传递为另一个参数,如下所示

[HttpPost]
public ActionResult Submit(SurveyResponseViewModel surveyResponse , int Id )
{

  surveyResponse.Id = Id
}

I have tested. 我测试过了 Its working fine. 它的工作正常。

    public ActionResult test1()
    {
        var model = new UserSurvey();
        model.Id = 10;
        return View(model);
    }
    [HttpPost]
    public ActionResult test1(SurveyResponseViewModel surveyResponse)
    {
        var x = surveyResponse.Id; // returns 10
        return View(new UserSurvey());
    }

    public class SurveyResponseViewModel
    {
        public int Id { get; set; }
        public Survey Survey { get; set; }
    }

    public class UserSurvey
    {
        public int Id { get; set; }
        public virtual Survey Survey { get; set; }
    }

    public class Survey
    {
        public string Steps { get; set; }
    }

@model TestWeb.Controllers.UserSurvey

@using (Html.BeginForm())
{
    <div class="container survey">
        @Html.HiddenFor(x=>x.Id)

        @Html.EditorFor(x => x.Survey.Steps)
    </div>

    <input type="submit" value="Submit" id="btnSubmit"/>
}

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

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