我试图将值从DropDownList
发送到数据库,我不知道这里出了什么问题,视图中的所有字段都已存储,但是从EditorFor
变为DropDownList
的字段不工作。
我的控制器代码:
// GET: Trainees/Create
public ActionResult Create()
{
ViewBag.PaymentF = new SelectList(db.Payments, "id", "payment_Name");
ViewBag.LevelList = db.Levels;
var model = new Trainees();
return View(model);
}
================================================ ====================
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create( [Bind(Include = "Nationality,FName,SName,LastName,AcademicNumber,Level,Department,PhoneNumber,Email,Hours,HourCost,Totaly,Note,payment_Name")] Trainees trainee)
{
if (ModelState.IsValid)
{
var senddata = new Trainees { };
db.Trainees.Add(trainee);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(trainee);
}
================================================ ==========
@Html.LabelFor(model => model.payment_Name, htmlAttributes: new { @class = "control-label col-md-1" })
<div class="col-md-3 jumbotron">
@Html.DropDownList("PaymentF", null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.payment_Name, "", new { @class = "text-danger" })
</div>
</div>
================================================ =========================
这是我的数据库结构:
答案 0 :(得分:0)
null
上方代码中看到的作为Dropdownlist中的第二个变量,我们通常会传递data
的源类型List<SelectListItem>
。请提及您如何绑定您的数据添加到下拉列表。
如果您的表单发布到action
,并且您的下拉列表中选择了一个值,那么您可以获得以下值
var itemValue = Request.Form["PaymentF"];