繁体   English   中英

DropDownList选择发布为空ASP.NET MVC

[英]DropDownList selection is posting as null ASP.NET MVC

我有一个从一个具有称为“ IncidentNumber”的列的数据库表中填充的DropDownList。 使用此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

 @Html.DropDownListFor(m => m.IncidentNumber, new SelectList(ViewBag.data, "IncidentNumber", "IncidentNumber"))

在Controller中,您必须使用IncidentNumber属性进行访问

好的,根据评论,我认为这会有所帮助。

由于IncidentNumberLandLostPersonDetail类的属性,因此您应该使用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 actionresult ..中,您将不再需要:

var items = db.IncidentNumbers.ToList();
ViewBag.data = new SelectList(items.Select(x => new { Text=x.IncidentNumber1, Id=x.Id, Value = x.IncidentNumber1 }).ToList());

请让我知道这可不可以帮你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM