C# MVC3 asp.net 错误:值不能为空。参数名称:下拉列表中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16995529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC3 asp.net error: Value cannot be null. Parameter name: items on dropdownlist
提问by Hariprasauth Ramamoorthy
I am getting a dump ONLY in the server and not in my local system when trying to post the data. There is a page which submits some value to the database. I have also modeled the dropdown in the page as mandatory. However, when clicking on "Create", instead of giving an error like "Missing"; it throws a dump.
尝试发布数据时,我仅在服务器中而不是在本地系统中获得转储。有一个页面向数据库提交一些值。我还将页面中的下拉列表建模为强制性的。但是,当单击“创建”时,而不是给出“丢失”之类的错误;它抛出一个转储。
Dump trace:
转储跟踪:
Value cannot be null.
Parameter name: items
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items
Source Error:
Line 65: </div>
Line 66: <div class="editor-field">
Line 67: @Html.DropDownListFor(x => x.ProjectName, new SelectList(Model.ProjectDetail, "ProjectName", "ProjectName"),"")
Line 68: <span runat="server" style="color:Red;" visible="false"> *</span>
Line 69: @Html.ValidationMessageFor(model => model.ProjectName)
Source File: d:\hosting178048\html\fbpm\fbpm\Views\User\Create.cshtml Line: 67
Stack Trace:
堆栈跟踪:
[ArgumentNullException: Value cannot be null. Parameter name: items] System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, IEnumerable selectedValues)
+289714 System.Web.Mvc.SelectList..ctor(IEnumerable items, String dataValueField, String dataTextField) +19 ASP._Page_Views_User_Create_cshtml.Execute() in d:\hosting178048\html\fbpm\fbpm\Views\User\Create.cshtml:67 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81 System.Web.WebPages.StartPage.RunPage() +17
The Controller code:
控制器代码:
public ActionResult Create()
{
var model = new UserDetail
{
ProjectDetail = db1.ProjectDetails.ToList()
};
return View(model);
}
//
// POST: /User/Create
[HttpPost]
public ActionResult Create(UserDetail userdetail)
{
if (ModelState.IsValid)
{
db.UserDetails.Add(userdetail);
db.SaveChanges();
return RedirectToAction("SearchCust");
}
return View(userdetail);
}
The view code:
视图代码:
@model fbpm.Models.UserDetail
@{
ViewBag.Title = "Create Customer";
}
<h2>Create Customer</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Customer Detail</legend>
<div id ="left" style="float:left; width:400px;">
<div class="editor-label">
@Html.LabelFor(model => model.UserID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserID)
<span runat="server" style="color:Red;" visible="false"> *</span>
@Html.ValidationMessageFor(model => model.UserID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
<span runat="server" style="color:Red;" visible="false"> *</span>
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PANNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PANNo)
@Html.ValidationMessageFor(model => model.PANNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailID)
@Html.ValidationMessageFor(model => model.EmailID)
</div>
<br />
<p>
<input type="submit" value="Create Customer" />
</p>
</div>
<div id = "left3" style="float:left; width:400px">
<div class="editor-label">
@Html.LabelFor(model => model.ProjectName)
</div>
<div class="editor-field">
@Html.DropDownListFor(x => x.ProjectName, new SelectList(Model.ProjectDetail, "ProjectName", "ProjectName"),"")
<span runat="server" style="color:Red;" visible="false"> *</span>
@Html.ValidationMessageFor(model => model.ProjectName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.BookedDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BookedDate)
<span runat="server" style="color:Red;" visible="false"> *</span>
@Html.ValidationMessageFor(model => model.BookedDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.BookedAmount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BookedAmount)
<span runat="server" style="color:Red;" visible="false"> *</span>
@Html.ValidationMessageFor(model => model.BookedAmount)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Contact1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Contact1)
@Html.ValidationMessageFor(model => model.Contact1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Contact2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Contact2)
@Html.ValidationMessageFor(model => model.Contact2)
</div>
</div>
<div id="left1" style="float:left; width:400px;">
<div class="editor-field">
@Html.HiddenFor(model => model.Role, new { @readonly = "readonly", @Value = "400" })
@Html.ValidationMessageFor(model => model.Role)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FullAddress)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.FullAddress)
@Html.ValidationMessageFor(model => model.FullAddress)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.State)
@Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Country)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Country)
@Html.ValidationMessageFor(model => model.Country)
</div>
</div>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "SearchCust")
</div>
I have searched a lot in the net and found that adding a viewbag for the project name in the submit action before returning the view owuld help; but, it didnt. Please can someone help?
我在网上搜索了很多,发现在返回视图之前在提交操作中为项目名称添加一个视图包会有所帮助;但是,它没有。请问有人可以帮忙吗?
Regards
问候
采纳答案by Tieson T.
I'm assuming you only see this exception when your insert fails; you then try to reuse the UserDetailmodel in the view for the same page.
我假设您只在插入失败时看到此异常;然后尝试UserDetail在同一页面的视图中重用模型。
The error you are seeing is due to the nature of working with HTTP - anything that is not directly bound to an input is notretained. So, when you attempt to rebuild the view, the list you are trying to bind the drop-down helper to is null, since UserDetail.ProjectDetailhas not been repopulated. You can fix this like so:
您看到的错误是由于使用 HTTP 的性质造成的 - 任何未直接绑定到输入的内容都不会保留。因此,当您尝试重建视图时,您尝试将下拉帮助程序绑定到的列表为空,因为UserDetail.ProjectDetail尚未重新填充。你可以像这样解决这个问题:
[HttpPost]
public ActionResult Create(UserDetail userdetail)
{
if (ModelState.IsValid)
{
db.UserDetails.Add(userdetail);
db.SaveChanges();
return RedirectToAction("SearchCust");
}
userdetail.ProjectDetail = db1.ProjectDetails.ToList();
return View(userdetail);
}
回答by flyfrog
I noticed that create button is a submit button
我注意到创建按钮是一个提交按钮
So it must call action with HttpPost attribute [HttpPost] public ActionResult Create(UserDetail userdetail)
所以它必须调用带有 HttpPost 属性的动作 [HttpPost] public ActionResult Create(UserDetail userdetail)
In this action, it returns View(userdetail);
在这个动作中,它返回 View(userdetail);
But this userdetail object is created by model binder from the submit data from browser. So it won't have values in ProjectDetail property.
但是这个 userdetail 对象是由模型绑定器从浏览器提交的数据中创建的。所以它在 ProjectDetail 属性中没有值。
You can step it through
你可以逐步通过

