为什么ViewBag对象名称必须与它所绑定的Drop Down名称相同?

时间:2017-08-31 01:46:41

标签: c# asp.net-mvc razor viewbag

我有一个Razor cshtml设置,我通过ViewBag发送一个SelectList。我的问题是ViewBag对象的命名,我想像这样发送它(见下文),但似乎并没有为我工作。

控制器:

ViewBag.EmailAddresses = new[]
    {
        new SelectListItem { Text = "Select an Email Address", Value = "" }
    }
    .Concat(
        new SelectList( db.EmailAddresses, "Oid", "Name", null )
    );

Razor观点:

@Html.DropDownList(
    "FromRecipientID", 
    ViewBag.EmailAddresses as SelectList, 
    null,
    htmlAttributes: new { @class = "form-control" }
)

但是,只有当我设置如下设置时它才有效:

控制器(工作):

ViewBag.FromRecipientID = new[]
    {
        new SelectListItem { Text = "Select an Email Address", Value = "" } 
    }
    .Concat(
        new SelectList( db.EmailAddresses, "Oid", "Name", null )
    );

Razor view(作品):

@Html.DropDownList(
    "FromRecipientID", 
    //Binding SelectList isn't required, assumption of ViewBag Name and Property
    null, 
    htmlAttributes: new { @class = "form-control" }
)

还有其他选择吗?我不确定我错过了什么。

编辑#1:

我已经能够在编辑时更新我的​​ViewModel,填充IEnumerable<SelectListItem> EmailAddresses以及EmailAddressID:

public ActionResult Edit(Guid? id)
{
    //...
    //Omitted information   

    //List of EmailMessages
    formView.EmailMessages = form.EmailMessages
        .Select(f =>
        {
            //Populate IEnumerable<SelectListItem>
            f.EmailAddresses = new SelectList(db.EmailAddresses, "Oid", "Name", f.FromRecipientID);
            return f;
        })
    ;

    //More omitted information
    //...

}

IEnumerable包含以下内容:

My Email    > id = 1
Your Email  > id = 2 (this should be the selected item)

//Note that the DropDownListFor examples below added "Select an Email Address" to the top of the list

以下Razor cshtml代码试图显示我尝试过的内容和结果,我一次只有一个DropDownListFor活跃(我注意到第一个实例是唯一一个&#34;可以&#34;正常工作)。

在我的PartialView中,我有FromRecipientID(Guid)或FromRecipient(EmailAddress对象):

<div class="form-group">
    @Html.LabelFor(model => model.FromRecipientID, "FromRecipientID", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">

        @* Works, selected ID 2 (because of Model.EmailAddesses) *@
        @* On Save error though, noted below *@
        @Html.DropDownListFor(
            model => model.FromRecipient, 
            Model.EmailAddresses, 
            "Select an Email Address", 
            htmlAttributes: new { @class = "form-control" })

        @* Failed, no ViewBag.FromRecipient (makes sense) *@
        @Html.DropDownListFor(
            model => model.FromRecipient, 
            null, 
            "Select an Email Address", 
            htmlAttributes: new { @class = "form-control" })

        @* Does not work, selected "Select an Email Address" *@
        @Html.DropDownListFor(
            model => model.FromRecipientID, 
            Model.EmailAddresses, 
            "Select an Email Address", 
            htmlAttributes: new { @class = "form-control" })

        @* Works, selected ID 2 (but because of ViewBag.FromRecipientID) *@
        @Html.DropDownListFor(
            model => model.FromRecipientID, 
            null, 
            "Select an Email Address", 
            htmlAttributes: new { @class = "form-control" })

        @Html.ValidationMessageFor(model => model.FromRecipientID, "", new { @class = "text-danger" })
    </div>
</div>

对于第一个示例,正确填充的那个(... model => model.FromRecipient, Model.EmailAddresses, "Select an Email Address" ...)没有在Save上工作,我收到以下错误:

System.InvalidOperationException: 
'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'FromRecipient'.'

这似乎来自ModelState无效,我得到以下内容:

{"The parameter conversion from type 'System.String' to type 'ContentPub.Models.Mail.EmailAddress' failed because no type converter can convert between these types."}

我明白了,我的价值是&#34; 2&#34;但是DropDownListFor试图设置model =&gt; model.FromRecipient。

我是否过度思考?

编辑#2

哇,讨厌报告这个,因为这是一个简单的修复/问题,但我需要......

我删除了旧的ViewBag设置:

//ViewBag.FromRecipientID = new[] { new SelectListItem { Text = "Select an Email Address", Value = "" } }
//    .Concat(new SelectList(db.EmailAddresses, "Oid", "Name", null));

正如我现在所看到的,这妨碍了我的测试。

这是正常工作的DropDownListFor

 @Html.DropDownListFor(
    model => model.FromRecipientID, 

    //When the ViewBag.FromRecipientID was active, Model.EmailAddesses wasn't being used
    // and was causing some type of conflict
    Model.EmailAddresses, 

    "Select an Email Address", 
    htmlAttributes: new { @class = "form-control" })

我会做更多测试(并在确认时回答我自己的问题),但我认为这是我一直需要的。

0 个答案:

没有答案