视图模型
namespace VendorFinancing.Web.Models
{
public class CreateApplicationViewModel
{
public Application Application { get; set; }
public List<Equipment> Equipments { get; set; }
public string SelectedVendorId;
public IEnumerable<SelectListItem> VendorList { get; set; }
}
}
控制器
public ActionResult Create()
{
var model = new CreateApplicationViewModel();
model.VendorList = new List<SelectListItem>() { new SelectListItem() { Text = "London", Value = "1" },
new SelectListItem() { Text = "New York", Value="2" } };
return View(model);
}
[HttpPost]
public ActionResult Create(CreateApplicationViewModel model)
{
return View(model);
}
查看
@using (Html.BeginForm("Create", "Application", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<div class="form-horizontal">
<h2>Submit Application </h2>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<h3>Pick Vendor</h3>
<div class="form-group">
<div class="col-md-2">
@Html.LabelFor(c => c.SelectedVendorId)
</div>
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedVendorId, Model.VendorList)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 pull-left">
<input type="submit" value="Create" class="btn btn-default pull-left" />
</div>
</div>
</div>
}
这是非常基本的创作。提交表单时SelectedVendorId
仍为空。我尝试了相同的例子,它仍然有效。我无法弄清楚原因。任何帮助都是适当的。
编辑:发现我的错误SelectedVendorId
没有得到和设置。在成为一个有效的房产之后。