我有绑定模型:
public class CreateBindingModel
{
public object1 object1 { get; set; }
public object2 object2 { get; set; }
}
我想通过控制器中的Create
形式传递它:
public ActionResult Create()
{
this.ViewBag.TaskId = new SelectList(this.db.Tasks, "Id", "Name");
return this.View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateBindingModel createBindingModel)
{
//body
}
我的Create.cshtml
如下:
@model namespace.Models.CreateBindingModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Object1</h4>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.object1.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.object1.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.object1.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<h4>Object 2</h4>
<div class="form-group">
@Html.LabelFor(model => model.object2.TaskId, "TaskId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("TaskId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.object2.TaskId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.object2.DaysToExpire, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.object2.DaysToExpire, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.object2.DaysToExpire, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
我的问题是,为什么我可以从上面通过这种形式在DaysToExpire
中获得object2
,但是却无法从object2的下拉列表中获得TaskId
(在{{1}中为null }控制器)?下拉列表会在我的表单中正确生成
Create
的一部分效果很好
编辑
在object1
类中创建属性时,
BindingModel
然后在public int TaskId { get; set; }
中重命名model.object2.TaskId
并将其更改为@HTML
,这样效果很好