MVC模型没有绑定帖子

时间:2016-02-26 00:49:42

标签: c# asp.net-mvc

无法弄清楚我做错了什么。当发布视图中的表单时,模型属性变为空。

模型

public class RegistrationModel
{
    public RegistrationModel()
    {
        Registration = new REGISTRATION();
        AddPayment = true;
    }
    public REGISTRATION Registration { get; set; }
    public bool AddPayment { get; set; }
}

视图

@model Client.Models.RegistrationModel

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(excludePropertyErrors: false)

    <div class="form-group">
        @Html.DropDownList("SECTION_ID", null, string.Empty, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.DropDownList("STUDENT_ID", null, string.Empty, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.DropDownList("STATUS_ID", null, string.Empty, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.CheckBoxFor(model => model.AddPayment)
    </div>

    <p>
        <input type="submit" class="btn btn-success" value="Create" />
    </p>
}

控制器

    public ActionResult Create()
    {
        //code to populate view dropdowns
        return View();
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(RegistrationModel model)
    {
        WriteFileLog(_logPath, Newtonsoft.Json.JsonConvert.SerializeObject(model));
    }

在控制器中,处理后期模型属性的“创建”操作为空。

注册类(由数据库中的EF自动生成):

public partial class REGISTRATION
{
    public REGISTRATION()
    {
        this.REGISTRATION_AUDIT = new HashSet<REGISTRATION_AUDIT>();
    }

    public int ID { get; set; }
    public int SECTION_ID { get; set; }
    public int STUDENT_ID { get; set; }
    public int STATUS_ID { get; set; }

    public virtual ICollection<REGISTRATION_AUDIT> REGISTRATION_AUDIT { get; set; }
    public virtual SECTION SECTION { get; set; }
    public virtual V_REGISTRATION_STATUS V_REGISTRATION_STATUS { get; set; }
    public virtual PERSON PERSON { get; set; }
}

1 个答案:

答案 0 :(得分:1)

我建议使用强类型助手,如下所示:

@Html.DropDownListFor(m => m.Registration.SECTION_ID, null, string.Empty, new { @class = "form-control" })

否则,您需要调整用于

的名称
@Html.DropDownList("Registration.SECTION_ID", null, string.Empty, new { @class = "form-control" })

您可以通过将注册课程的成员复制到视图模型中来替换注册属性,从而简化您的工作。

正如@StephenMuecke指出的那样,你从模型/标记中遗漏了一些部分。您正在使用的DropDownList helper的模板是

DropDownListFor(
    [model property to bind], 
    [collection of possible values to bind], 
    [option label], 
    [HTML attributes])

为第二个参数传递null意味着您没有值来填充生成的<select>元素,并且通常应该生成异常。

我不喜欢使用ViewBag将集合传递到视图中,所以我推荐像

这样的东西
public class RegistrationModel
{
    public RegistrationModel()
    {
        Registration = new REGISTRATION();
        AddPayment = true;
    }
    public REGISTRATION Registration { get; set; }
    public bool AddPayment { get; set; }

    public SelectList Sections { get; set; }

    public SelectList Students { get; set; }

    public SelectList Statuses { get; set; }
}

然后相应地调整标记:

@Html.DropDownListFor(m => m.Registration.SECTION_ID, Model.Sections, string.Empty, new { @class = "form-control" })