问题
I am having an issue with saving the edited fields in my MVC project. When I click the submit button I get an error
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 63: Line 64: Line 65: @Html.DropDownListFor(model => model.EventTypeID, new SelectList(ViewBag.EventTypes, "EventTypeID", "EventType1"), "Choose") Line 66: Line 67:
I have tried some things that I have seen on the internet but it doesn't seem to be doing anything for me :( Help would be appreciated thank you!
this is my controller When I put a break point here it never triggers.
[HttpPost]
public ActionResult IndexPST(Event_Setup es)
{
db.Event_Setup.Add(es);
db.SaveChanges();
return View("Index",es);
}
And this is my View
@model DixonGroupInc.Models.Event_Setup
<link href="~/Content/EventManagement.css" rel="stylesheet" />
@using (Html.BeginForm("IndexPST", "EventManagement", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div id="cntr">
<div id="emLoc">
<table id="ldTbl">
<thead>
<tr>
<th class="ln">
<img src="~/Images/4.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="chosen space">EventMetadata
</td>
<td class="space">Client / Vendor
</td>
<td class="space">Venu info
</td>
<td class="space">Sponsored Participants
</td>
<td class="space">Event Services
</td>
</tr>
</tbody>
</table>
</div>
<div id="emSetup">
Setup Meeting
</div>
<div id="emTable">
<table class="myTbl">
<tr>
<td class="lblEvent clm1">
@Html.Label("Event Type")
</td>
<td class="clm2">
@Html.DropDownListFor(model => model.EventTypeID, new SelectList(ViewBag.EventTypes, "EventTypeID", "EventType1"), "Choose")
</td>
<td class="clm3">
@{Html.RenderAction("EventType", "EventManagement");}
</td>
</tr>
<tr class="rows">
<td class="lblEvent clm1">
@Html.Label("Event Title")
</td>
<td class="clm2">
@Html.EditorFor(model => model.EventTitle)
@Html.ValidationMessageFor(model => model.EventTitle)
</td>
</tr>
<tr>
<td class="lblEvent clm1">
@Html.Label("Event Identifier")
</td>
<td class="clm2">
@Html.EditorFor(model => model.EventIdentifier)
@Html.ValidationMessageFor(model => model.EventIdentifier)
</td>
</tr>
</table>
<table class="dateTbl">
<tr class="rows">
<td class="lblEvent clm1">
@Html.Label("Event Date From")
</td>
<td class="clm2">
@Html.TextBoxFor(model => model.EventDateFrom, new { @class = "dateFrom" })
@Html.ValidationMessageFor(model => model.EventDateFrom)
</td>
<td class="lblEvent">
@Html.Label("To")
</td>
<td class="clm4">
@Html.TextBoxFor(model => model.EventDateTo, new { @class = "dateTo" })
@Html.ValidationMessageFor(model => model.EventDateTo)
</td>
</tr>
</table>
<table class="myTbl">
<tr>
<td class="lblEvent clm1">
@Html.Label("Event Description")
</td>
<td class="clm2">
@Html.TextAreaFor(model => model.EventDescription, new
{
id = "taED"
})
<div>
<span id="charLeft2"></span>characters remaining.
</div>
@Html.ValidationMessageFor(model => model.EventDescription)
</td>
</tr>
<tr class="rows">
<td class="lblEvent clm1">
@Html.Label("Custom Message")
</td>
<td class="clm2">
@Html.TextAreaFor(model => model.CustomMessage, new
{
id = "taCM"
})
<div>
<span id="charLeft1"></span>characters remaining.
</div>
@Html.ValidationMessageFor(model => model.CustomMessage)
</td>
</tr>
<tr>
<td class="lblEvent clm1">
@Html.Label("Instructions")
</td>
<td class="clm2">
@Html.TextAreaFor(model => model.Instructions, new
{
id = "taInstrct"
})
<div>
<span id="charLeft"></span>characters remaining.
</div>
@Html.ValidationMessageFor(model => model.Instructions)
</td>
</tr>
</table>
<table class="myTbl">
<tr class="rows">
<td>
@{Html.RenderAction("AddTrack", "EventManagement");}
</td>
</tr>
<tr>
<td>
@{Html.RenderAction("EventTrack", "EventManagement");}
</td>
</tr>
</table>
<table class="myTbl">
<tr class="rows">
<td class="lblEvent clm1">
@Html.Label("CFP Pocess")
</td>
<td class="clm2">
@Html.RadioButtonFor(model => model.CFPRequired, "true") Yes
</td>
<td class="clm3">
@Html.RadioButtonFor(model => model.CFPRequired, "false") No
</td>
</tr>
</table>
</div>
<div id="emSubmit">
<input type="submit" value="Save & Next" id="submit" />
</div>
</div>
}
回答1:
Viewbag can be unreliable so generally it isn't recommended to pass your drop down list data through the viewbag. I would recommend adding it to your view model instead. In your Event_Setup class make sure that you have
public int EventTypeID { get; set; }
and
public List<SelectListItem> EventTypeList { get; set; }
on your controller get you can set EventTypeID to set a default on the drop down so your controller should have something like
Event_Setup es = new Event_Setup();
es.EventTypeID = //default value if you want;
es.EventTypeList = //Method call here where List<SelectListItem> is returned with your event list
then on your view you can change your drop down to
@Html.DropDownListFor(x => x.EventTypeID, Model.EventTypeList)
来源:https://stackoverflow.com/questions/25434762/issue-saving-edited-data-ddl-value-can-not-be-null