我使用断点调试,我的ClientsId总是空的,并且显示在我的支付索引上总是我的Dropdownlist列表的第一个值
型号:
public class Payments
{
[Key]
public int PaymentsId { get; set; }
public int ClientId { get; set; }
public virtual Client Client { get; set; }
}ViewModel:
public class PaymentsViewModel
{
[Required(ErrorMessage = "Please select a client")]
[Display(Name = "Client")]
public int SelectedClient { get; set; }
public IEnumerable<SelectListItem> Client { get; set; }
}获取控制器:
public ActionResult Create(Payments model)
{
var liste= new PaymentsViewModel
{
Clients = new SelectList(db.ClientList, "ClientId", "ClientName")
};
return View(liste);
}岗控员:
public ActionResult Create([Bind(Include = "....")] PaymentsViewModel model)
{
if (ModelState.IsValid)
{
model.PaymentsCreate();
return RedirectToAction("Index", "Payments");
}
return View(model);
}创建视图:
@Html.DropDownListFor(m => m.SelectedClient, Model.Clients, "-Please select-", new { @class = "form-control" })
</div>
</div>编辑控制器(GET):
public ActionResult Edit(int? id, PaymentsViewModel model)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Payments payments = db.PaymentsList.Find(id);
if (payments == null)
{
return HttpNotFound();
}
return View();
}编辑控制器(POST)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PaymentsId,Paymentnumber,PaymentDate,Amount,Discount,Reference,Total")] Payments payments)
{
if (ModelState.IsValid)
{
db.Entry(payments).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(payments);
}发布于 2016-02-14 17:09:01
您应该在ClientsId函数(如: ClientsId = model.SelectedClient )上添加来自model.SelectedClient的PaymentsCreate初始化。然后,您需要在创建(post)方法中将SelectedClient字符串添加到属性枚举以进行绑定(包括.属性
https://stackoverflow.com/questions/35387310
复制相似问题