簡體   English   中英

MVC3 asp.net錯誤:值不能為null。參數名稱:下拉列表中的項目

[英]MVC3 asp.net error: Value cannot be null. Parameter name: items on dropdownlist

在嘗試發布數據時,我只在服務器中獲取轉儲,而不是在本地系統中。 有一個頁面向數據庫提交了一些值。 我還將頁面中的下拉列表建模為強制性。 但是,當點擊“創建”時,而不是給出“失蹤”之類的錯誤; 它拋出一個轉儲。

轉儲跟蹤:

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

我在網上搜索了很多,發現在返回視圖owuld幫助之前在提交操作中為項目名稱添加了一個視圖包; 但是,它沒有。 請有人幫忙嗎?

問候

我假設您的插入失敗時只看到此異常; 然后嘗試在同一頁面的視圖中重用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);
}

我注意到創建按鈕是一個提交按鈕

所以它必須用HttpPost屬性調用動作[HttpPost] public ActionResult Create(UserDetail userdetail)

在此操作中,它返回View(userdetail);

但是這個userdetail對象是由來自瀏覽器的提交數據的模型綁定器創建的。 因此它在ProjectDetail屬性中沒有值。

你可以一步一步

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM