简体   繁体   English

DropDownList选择发布为空ASP.NET MVC

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

I have a DropDownList that is populated from a database table that has one column called "IncidentNumber". 我有一个从一个具有称为“ IncidentNumber”的列的数据库表中填充的DropDownList。 When using this DropDownList to post as part of a form into a different database table the application is throwing an exception because the DropDownList selection is posting as null. 使用此DropDownList作为表单的一部分发布到另一个数据库表中时,应用程序将引发异常,因为DropDownList选择发布为null。 Any help here would be much appreciated! 在这里的任何帮助将不胜感激!

Controller Action 控制器动作

 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();
            }
         }

Code that is in the View 视图中的代码

<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>

EDIT: Incident Number class: 编辑:事件编号类别:

    public partial class IncidentNumber
{
    public int Id { get; set; }
    public string IncidentNumber1 { get; set; }
}

My controller currently looks like: 我的控制器当前看起来像:

    [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;
            }
        }

And the view currently looks like: 当前视图如下:

@Html.DropDownListFor(model => model.Incident_Number, new SelectList(ViewBag.data, "IncidentNumber1", "IncidentNumber1"))

I am currently getting an exception which says that IncidentNumber1 does not exist in System.Web.Mvc.SelectListItem 我目前遇到异常,该异常表明System.Web.Mvc.SelectListItem中不存在IncidentNumber1

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

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

Okay, based from the comments, I think this will help. 好的,根据评论,我认为这会有所帮助。

Since, IncidentNumber is a property of LandLostPersonDetail class, then you should use DropDownListFor instead of DropDownList . 由于IncidentNumberLandLostPersonDetail类的属性,因此您应该使用DropDownListFor而不是DropDownList DropDownListFor allows for client-side validation based on your model.. so if in your LandLostPersonDetail class.. let's say that IncidentNumber is required.. then if there is no value for that when the form is submitted then validation will occur on the client-side rather than having to wait and go to if (ModelState.IsValid) on the server-side. DropDownListFor允许根据您的模型进行客户端验证..因此,如果在您的LandLostPersonDetail类中..假设需要IncidentNumber ..那么如果提交表单时没有任何价值,那么验证将在客户端上进行-不必等待服务器端的if (ModelState.IsValid)

Here is what I recommend changing: 这是我建议更改的内容:

In your HttpGet Action 在您的HttpGet操作中

ViewBag.IncidentNumberSelection = new SelectList(db.IncidentNumbers.ToList(), "IncidentNumber1", "IncidentNumber1");

In your View 在您看来

<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>

Then, in your HttpPost actionresult.. you will no longer need: 然后,在您的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());

Please let me know if this helps. 请让我知道这可不可以帮你。

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

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