System.ArgumentNullException:值不能为null。参数名称:items

时间:2014-02-25 10:31:09

标签: asp.net-mvc-4 html-select

我在此行收到错误System.ArgumentNullException: Value cannot be null. Parameter name: items

@Html.DropDownList("empnames", new SelectList(ViewBag.empnames), "Select");

我想从选定的Emp_id获取相应的Emp_Name,并希望更新我的表Enq_Submission

我的模型,控制器和视图如下:

型号:

namespace MvcConQuery.Models
{

[Table("Enq_Submission")]
public class EnquiryModel
{
private ConQueryDataClassesDataContext dc = new ConQueryDataClassesDataContext();
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Int32 Enq_id { get; set; }
[Required]
[Display(Name="Name")]
public string CustomerName { get; set; }
[ReadOnly(true)]
public string Date
{
get
{
DateTime Date = DateTime.Now;
return Date.ToString("yyyy-MM-dd"); ;
}

set{}

}


[Required]
[Display(Name = "Region")]
public string Region { get; set; }
[Required]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone number format is not valid.")]
[Display(Name = "Phone number")]
public string Ph_No { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email_id")]
public string Email_id { get; set; }
[Required]
[Display(Name = "Address")]
public string Address { get; set; }
[Required]
[Display(Name = "Query")]
public string Query { get; set; }
public string Referral { get; set; }
public string Feedback { get; set; }
public string Status { get; set; }
public Int32? Emp_id { get; set; }
public string FollowUpDate { get; set; }
public List<EmployeeModel> Employees { get; set; }

}}

namespace MvcConQuery.Models
{
[Table("Employee_Details")]
public class EmployeeModel
{

[Key,Column(Order=0)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
//[ForeignKey("EnquiryModel")]
public Int32 Emp_id { get; set; }
public string Emp_Name{ get; set; }
//[Key,Column(Order=1)]
public string Region { get; set; }
//[ForeignKey("Region")]
public string Emp_PhNo { get; set; }
public string Emp_Address { get; set; }
public List<EnquiryModel> Enquires { get; set; }


}

}

控制器:

public ActionResult Edit(int id)
{

EnquiryModel enquirymodel = db.Enquires.Find(id);
if (enquirymodel == null)
{
return HttpNotFound();
}
var rgn=enquirymodel.Region;
var empnames = (from ename in dc.GetTable<Employee_Detail>() where ename.Region ==rgn select ename.Emp_Name).ToList();
ViewBag.empnames = empnames;

return View(enquirymodel);

}

[HttpPost]
public ActionResult Edit(EnquiryModel enquirymodel,string empnames)
{
if (ModelState.IsValid)
{

var empid = (from eid in dc.GetTable<Employee_Detail>() where eid.Emp_Name == empnames select eid.Emp_id).First();
enquirymodel.Emp_id = empid;
db.SaveChanges();
}
return View(enquirymodel);
}

查看:

@model MvcConQuery.Models.EnquiryModel

@{
ViewBag.Title = "Edit";

}
<style>
.myClass label{
font-weight:bold;
}
</style>

<h2>Allocate Employee</h2>

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)

<fieldset>
<legend>EnquiryModel</legend>
@Html.HiddenFor(model => model.Enq_id)
@Html.HiddenFor(model => model.CustomerName)
@Html.HiddenFor(model => model.Date)
@Html.HiddenFor(model => model.Region)
@Html.HiddenFor(model => model.Ph_No)
@Html.HiddenFor(model => model.Email_id)
@Html.HiddenFor(model => model.Address)
@Html.HiddenFor(model => model.Query)
@Html.HiddenFor(model => model.Referral)
@Html.HiddenFor(model => model.Feedback)
@Html.HiddenFor(model => model.Status)
@Html.HiddenFor(model => model.FollowUpDate)
@Html.HiddenFor(Model => Model.Emp_id);


<div class="editor-label">
@Html.LabelFor(model => model.CustomerName, new { @class = "label" })
</div>

@Html.DisplayFor(model => model.CustomerName)

<div class="editor-label">
@Html.LabelFor(model => model.Region, new { @class = "label" })
</div>

@Html.DisplayFor(model => model.Region)

<div class="editor-label">
@Html.LabelFor(model => model.Ph_No, new { @class = "label" })
</div>

@Html.DisplayFor(model => model.Ph_No)

<div class="editor-label">
@Html.LabelFor(model => model.Email_id, new { @class = "label" })
</div>

@Html.DisplayFor(model => model.Email_id)

<div class="editor-label">
@Html.LabelFor(model => model.Address, new { @class = "label" })
</div>

@Html.DisplayFor(model => model.Address)<div class="editor-label">

@Html.LabelFor(model => model.Query, new { @class = "label" })
</div>

@Html.DisplayFor(model => model.Query)


@Html.Label("Select Employee", new { @class = "label" })
@Html.DropDownList("empnames", new SelectList(ViewBag.empnames), "Select");

<p>
<input type="submit" value="Allocate" name="Submit"/>
</p>

</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我猜你在Edit视图中。

与第一个Edit操作相反,第二个没有这一行:

var empnames = (from ename in dc.GetTable<Employee_Detail>() where ename.Region ==rgn select ename.Emp_Name).ToList();
ViewBag.empnames = empnames;

你能检查一下是否有助于添加它?

修改

我认为你不了解MVC的内部机制。当您返回View(...)时,它会使用与方法相同的名称呈现视图(在您的情况下为Edit)。由于两者都渲染Edit,因此需要填充empnames ViewBag值。

您还可以尝试在第二个Edit中返回此内容:

ViewBag.empnames = new List<Employee_Detail>();

答案 1 :(得分:1)

问题是'编辑'期望填充empnames ViewBag值。 然后,不更新表背后的原因是缺少控制器中的一行代码,即:

db.Entry(enquirymodel).State = EntityState.Modified;

[HttpPost]

    public ActionResult Edit(EnquiryModel enquirymodel,string empnames)
    {
        if (ModelState.IsValid)
        {
            var rgn = enquirymodel.Region;
            var empnamess = (from ename in dc.GetTable<Employee_Detail>() where ename.Region == rgn select ename.Emp_Name).ToList();
            ViewBag.empnames = empnamess;

      var empid = (from eid in dc.GetTable<Employee_Detail>() where eid.Emp_Name == empnames select eid.Emp_id).First();

            db.Entry(enquirymodel).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(enquirymodel);
    }