C# MVC4模型绑定返回null

C# MVC4模型绑定返回null,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我在MVC中的模型绑定方面遇到了问题。我有一门课: public class UserSurvey { public int Id { get; set; } public virtual Survey Survey { get; set; } } 哪个是视图的模型: @model SurveyR.Model.UserSurvey <form id="surveyForm"> <div class="container survey">

我在MVC中的模型绑定方面遇到了问题。我有一门课:

public class UserSurvey
{
    public int Id { get; set; }
    public virtual Survey Survey { get; set; }
}
哪个是视图的模型:

@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>
调试submit时,surveyResponse.Survey对象按其应为的方式填充,但surveyResponse.Id值应为1时为0

我可以看到Id=1在submit中被传回,但是模型绑定似乎没有将其连接起来

任何帮助都将不胜感激

凯文

编辑:呈现的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" />

因此,如果我使用开发工具查看的话,该值会显示在那里,并在提交中传递


编辑2:dev tools中的表单数据肯定包含“Id:1”。

您的代码似乎没有问题。请尝试将Id值作为另一个参数显式传递,如下所示

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

  surveyResponse.Id = Id
}

我已经测试过了。工作正常

    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"/>
}
public ActionResult test1()
{
var model=newusersurvey();
模型Id=10;
返回视图(模型);
}
[HttpPost]
公共行动结果测试1(调查响应环境模型调查响应)
{
var x=surveyResponse.Id;//返回10
返回视图(newusersurvey());
}
公共类调查响应环境模型
{
公共int Id{get;set;}
公共调查{get;set;}
}
公共类用户调查
{
公共int Id{get;set;}
公共虚拟调查{get;set;}
}
公营班级调查
{
公共字符串步骤{get;set;}
}
@模型TestWeb.Controllers.UserSurvey
@使用(Html.BeginForm())
{
@Html.HiddenFor(x=>x.Id)
@EditorFor(x=>x.Survey.Steps)
}

渲染视图时是否填充
id
?请共享呈现的标记。是否尝试使用Html.Begin语句?查看一下,如果您的按钮不是
submit
按钮,您需要通过此表单的Get操作传递IEnumerable调查的默认列表,您是如何提交表单的?如上所述,最好使用
Html.BeginForm
,但这当然不是必需的。按钮是提交按钮吗?然后控制器就拿起了HttpPost?我现在尝试了Html.BeginForm,没有任何更改。不需要定义默认构造函数。默认情况下,类有这个构造函数。。这个默认构造函数如何帮助解决上述问题?这对我没有帮助,它会出错,因为没有传入Id值。是否使用Html。在呈现视图时开始?Id将与控件的name属性值相同。因此,不要更改参数name Id。不,我称它为Id,但它仍然不能工作。不,我只是在使用一个表单,而不是Html。Beginform我相信它肯定会工作。如果出现错误“id值未传递”,请尝试将id更改为可选参数,然后重试。
    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"/>
}