c# – MVC3 asp.net错误:值不能为空.参数名称:下拉列表中的项目

前端之家收集整理的这篇文章主要介绍了c# – MVC3 asp.net错误:值不能为空.参数名称:下拉列表中的项目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试发布数据时,我只能在服务器中收到转储,而不是在本地系统中.有一个页面数据库提交一些值.我也将页面中的下拉菜单建模为强制性的.但是,当点击“创建”时,而不是像“Missing”一样发生错误;它会抛出一个垃圾.

转储痕迹:

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:\hosting\11178048\html\fbpm\fbpm\Views\User\Create.cshtml    Line: 67

堆栈跟踪:

[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 dataTextField) +19    ASP._Page_Views_User_Create_cshtml.Execute() in d:\hosting\11178048\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

控制器代码

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

视图代码

@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,"")
            <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>

我在网上搜索了很多,发现在提交操作之前添加一个项目名称的viewbag,然后返回视图owuld help;但是,它没有.请有人帮忙吗?

问候

解决方法

我假设你在插入失败时只看到这个异常;然后尝试在同一页面的视图中重用UserDetail模型.

您所看到的错误是由于使用HTTP的性质 – 不直接绑定到输入的任何内容都不会保留.因此,当您尝试重建视图时,您尝试绑定下拉帮助器的列表为null,因为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);
}

猜你在找的C#相关文章