保存已编辑数据的问题:DDL值不能为空

时间:2014-08-21 19:49:46

标签: asp.net-mvc-4 edit

我遇到了在MVC项目中保存已编辑字段的问题。当我单击提交按钮时出现错误


值不能为空。 参数名称:items   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentNullException:值不能为null。 参数名称:items

来源错误:

第63行: 第64行: 第65行:@ Html.DropDownListFor(model => model.EventTypeID,new SelectList(ViewBag.EventTypes," EventTypeID"," EventType1"),"选择" ) 第66行: 第67行:


我尝试了一些我在互联网上看到过的东西,但它似乎并没有为我做任何事情:(感谢您的帮助!谢谢!

这是我的控制器当我在此处设置断点时,它永远不会触发。

    [HttpPost]
    public ActionResult IndexPST(Event_Setup es) 
    {
        db.Event_Setup.Add(es);
        db.SaveChanges();
       return View("Index",es);
    }

这是我的观点

@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 个答案:

答案 0 :(得分:0)

Viewbag可能不可靠,因此通常不建议您通过viewbag传递下拉列表数据。我建议将其添加到您的视图模型中。在Event_Setup类中,确保您有

public int EventTypeID { get; set; }

public List<SelectListItem> EventTypeList { get; set; }
控制器上的

让你可以设置EventTypeID来设置下拉列表的默认设置,这样你的控制器应该有类似的东西

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

然后在您的视图中,您可以将下拉菜单更改为

@Html.DropDownListFor(x => x.EventTypeID, Model.EventTypeList)