2013-06-08 13 views
10

Dostaję zrzut tylko na serwerze, a nie w moim systemie lokalnym podczas próby opublikowania danych. Istnieje strona, która przesyła pewną wartość do bazy danych. Również wymodelowałem rozwijanie strony jako obowiązkowe. Jednak po kliknięciu "Utwórz", zamiast podawać błąd typu "Brak"; rzuca zrzut. śladBłąd asp.net MVC3: wartość nie może być pusta. Nazwa parametru: elementy na liście rozwijanej

przegubowe:

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 

stosu Ślad:

[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 

Kod Kontroler:

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

Kod widok:

@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> 

Szukałem dużo w sieci i stwierdziłem, że dodanie widoku do nazwy projektu w akcji przesyłania przed zwróceniem widoku pomocy owuld; ale tak się nie stało. Czy ktoś może pomóc?

Pozdrowienia

Odpowiedz

17

Jestem zakładając, widzisz tylko ten wyjątek, gdy wkładka nie; następnie próbujesz ponownie użyć modelu UserDetail w widoku dla tej samej strony.

Błąd, który widzisz, wynika z natury pracy z protokołem HTTP - wszystko, co nie jest bezpośrednio związane z danymi wejściowymi, zostało zatrzymane na , a nie na. Tak więc przy próbie odbudowania widoku lista, do której próbujesz powiązać pomocnika rozwijanego, ma wartość NULL, ponieważ UserDetail.ProjectDetail nie została ponownie zaludniona. Możesz to naprawić tak:

[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); 
} 
+2

Amazing !!!!!!!!!!!!! stukrotne dzięki!! –

+1

De nada. Jeśli jesteś nowicjuszem w MVC, przydatne może okazać się tutorial NerdDinner: http://www.wrox.com/WileyCDA/Section/id-321793.html. W przeciwnym razie jest to najlepsza książka, którą znalazłem: http://www.amazon.com/PROAS-NET-MVC-Adam-Freeman/dp/1430242361. –

+0

Działa.Błąd był nieco błądzący, ponieważ został zgłoszony na dodanie pierwszego błędu sprawdzania poprawności w moim niestandardowym walidatorze DTO, który nie ma nic wspólnego z kontrolą listy wyboru. –

1

zauważyłem, że przycisk tworzenia jest przycisk

Więc to musi wywołać działanie złożyć z HttpPost przypisują [HttpPost] Tworzenie ActionResult publicznych (UserDetail userdetail)

W tej akcji zwraca View (userdetail);

Ale ten obiekt userdetail jest tworzony przez model spoiwa z przesłanych danych z przeglądarki. Tak więc nie będzie mieć wartości we właściwości ProjectDetail.

Można ją zwiększyć przez

+0

masz na myśli, mogę tylko zwrócić View() zamiast widoku (usedetail)? –

+0

Mam na myśli, że musisz umieścić punkt przerwania w [HttpPost] publicznym ActionResult Create (UserDetail userdetail), aby sprawdzić, czy ma on wartość dla ProjectDetail. Jeśli nie, przeładuj ProjectDetail w tej akcji. – flyfrog

+0

Ok. W rzeczywistości zrzutu występuje tylko na serwerze. Czy możesz mi powiedzieć, jak mogę go ponownie załadować w tej akcji? –

Powiązane problemy