我有一个DropDownList,它是从一个数据库表中填充的,该表具有一个称为“ IncidentNumber”的列。使用此DropDownList作为表单的一部分发布到另一个数据库表中时,应用程序将引发异常,因为DropDownList选择发布为null。这里的任何帮助将不胜感激!
控制器动作
var items = db.ViewIncidentNumbers.ToList();
if (items != null)
{
ViewBag.data = items;
}
if (ModelState.IsValid)
{
using (LogInformationEntities4 dc = new LogInformationEntities4())
{
dc.LandLostPersonDetails.Add(land);
dc.SaveChanges();
}
}
视图中的代码
<div class="form-group">
@Html.LabelFor(model => model.Incident_Number, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("IncidentNumber", new SelectList(ViewBag.data, "IncidentNumber", "IncidentNumber"))
</div>
</div>
编辑: 事件编号类别:
public partial class IncidentNumber
{
public int Id { get; set; }
public string IncidentNumber1 { get; set; }
}
我的控制器当前如下所示:
[HttpGet]
public ActionResult NewLandMissingPerson()
{
string teamsession = string.Empty;
string username = string.Empty;
teamsession = Convert.ToString(Session["Callsign"]);
username = Convert.ToString(Session["Username"]);
LogInformationEntities4 dc = new LogInformationEntities4();
var items = dc.IncidentNumbers.ToList();
ViewBag.data = new SelectList(items.Select(x => new { Text = x.IncidentNumber1, Id = x.Id, Value = x.IncidentNumber1 }).ToList());
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NewLandMissingPerson(LandLostPersonDetail land)
{
LogInformationEntities4 db = new LogInformationEntities4();
bool Status = false;
string response = "";
var items = db.IncidentNumbers.ToList();
ViewBag.data = new SelectList(items.Select(x => new { Text=x.IncidentNumber1, Id=x.Id, Value = x.IncidentNumber1 }).ToList());
if (ModelState.IsValid)
{
using (LogInformationEntities4 dc = new LogInformationEntities4())
{
dc.LandLostPersonDetails.Add(land);
dc.SaveChanges();
response = "New Missing Person Log Added.";
Status = true;
}
}
当前视图如下:
@Html.DropDownListFor(model => model.Incident_Number, new SelectList(ViewBag.data, "IncidentNumber1", "IncidentNumber1"))
我目前遇到一个异常,该异常表明System.Web.Mvc.SelectListItem中不存在IncidentNumber1
答案 0 :(得分:1)
@Html.DropDownListFor(m => m.IncidentNumber, new SelectList(ViewBag.data, "IncidentNumber", "IncidentNumber"))
在Controller中,您必须使用 IncidentNumber 属性
进行访问答案 1 :(得分:1)
好的,根据评论,我认为这会有所帮助。
由于IncidentNumber
是LandLostPersonDetail
类的属性,因此您应该使用DropDownListFor
而不是DropDownList
。 DropDownListFor
允许基于您的模型进行客户端验证..因此,如果在您的LandLostPersonDetail
类中..假设需要IncidentNumber
..那么在这种情况下没有任何价值表单提交后,验证将在客户端进行,而不必等待并在服务器端转到if (ModelState.IsValid)
。
这是我建议更改的内容:
在您的HttpGet操作中
ViewBag.IncidentNumberSelection = new SelectList(db.IncidentNumbers.ToList(), "IncidentNumber1", "IncidentNumber1");
在您的视图中
<div class="form-group">
@Html.LabelFor(model => model.Incident_Number, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Incident_Number,(IEnumerable<SelectListItem>)ViewBag.IncidentNumberSelection, "-- Select Incident Number --", new { @class = "form-control" })
</div>
</div>
然后,在您的HttpPost
操作结果中,您将不再需要:
var items = db.IncidentNumbers.ToList();
ViewBag.data = new SelectList(items.Select(x => new { Text=x.IncidentNumber1, Id=x.Id, Value = x.IncidentNumber1 }).ToList());
请告诉我这是否有帮助。