我正在尝试设置属性的值,该值位于项目的模型中。 我想要的是在下拉列表中选择一个值,当我点击create时,它将选择的值设置为属性的值,这显然将发生在相应控制器的“ create”操作中。问题是我无法在选择列表中使用“ .SelectedValue”并将其设置为模型的属性。
有两种“创建”操作方法,一种是参数化的,另一种是未参数化的。前一种将数据保存到表中,而后一种获取值。我在未参数化的方法中创建了一个整数列表,然后尝试将其放入ViewBag(工作正常)。当我运行代码时,视图起作用,并且下拉列表由列表项填充。但是,在输入必要的详细信息之后,我按Enter键,尽管我已将模型的属性设置为下拉列表的SelectedValue,但它保持为NULL。
型号:
namespace Test.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class LabAsign
{
[Display(Name = "ID")]
public int LbAs_id { get; set; }
[Display(Name = "Organization ID")]
public Nullable<int> LbAs_orgId { get; set; }
[Display(Name = "Labor ID")]
public Nullable<int> LbAs_LabId { get; set; }
}
控制器
public static int LabId
public ActionResult Create()
{
ViewBag.LbAs_CmplntId = new SelectList(db.Complaints, "Cmplnt_Id");
var list = new List<int>();
foreach (var item in db.Labors)
{
string c = item.Lab_OrgId.ToString();
if (c == HomeController.c_orgid.ToString())
{
list.Add(item.Lab_id);
}
}
var selectList = new SelectList(list);
Session["labid"] = list;
ViewBag.LbAsLabId = selectList;
//ViewBag.LbAs_LabId = new SelectList(db.Labors, "Lab_id",
"Lab_name");
ViewBag.LbAs_orgId = new SelectList(db.Organizations, "org_id",
"org_name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include =
"LbAs_id,LbAs_orgId,LbAs_LabId,LbAs_date,LbAs_CmplntId,LbAs_complStatus")]
LabAsign labAsign)
{
if (ModelState.IsValid)
{
var selectList = new
SelectList((System.Collections.IEnumerable)Session["labid"]);
LabId = (int)selectList.SelectedValue;
labAsign.LbAs_LabId = LabId;
db.LabAsigns.Add(labAsign);
db.SaveChanges();
return RedirectToAction("IndexLbAsOrg");
}
ViewBag.LbAs_CmplntId = new SelectList(db.Complaints,
"Cmplnt_Id","complnt_content", labAsign.LbAs_CmplntId);
ViewBag.LbAs_LabId = new SelectList(db.Labors, "Lab_id",
"Lab_name", labAsign.LbAs_LabId);
ViewBag.LbAs_orgId = new SelectList(db.Organizations, "org_id",
"org_name", labAsign.LbAs_orgId);
return View(labAsign);
}
查看:
@model Test.Models.LabAsign
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>LabAsign</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger"
})
<div class="form-group">
@Html.LabelFor(model => model.LbAs_orgId, "Organization
ID", htmlAttributes: new { @class = "control-label col-md-2"@*, @style =
"display:none;"*@ })
<div class="col-md-10">
@Html.EditorFor(model => model.LbAs_orgId, new {
htmlAttributes = new { @class = "form-control"@*, @style =
"display:none;"*@,
@Value = Session["UserId"]} })
@Html.ValidationMessageFor(model => model.LbAs_orgId,
"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LbAs_LabId, "LbAs_LabId",
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("LbAsLabId", null, htmlAttributes:
new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.LbAs_LabId,
"", new { @class = "text-danger" })
</div>
</div>
如您所见,我正在使用Sessions将列表从一个create方法传输到另一个create方法,声明一个新的selectlist并使用selectlist中的SelectedValue来更新属性,但这会引发此异常:
System.NullReferenceException: Object reference not set to an instance
of an object.
当我尝试通过将视图的值设置为直接设置下拉列表的值时:
@Html.DropDownList("LbAsLabId", null, htmlAttributes: new { @class =
"form-control", @Value = Session["labid"] })
属性值设置为NULL,但不会引发任何异常