简体   繁体   English

ASP.NET MVC 5 站点 - 编辑视图未选择 DropDownListFor 中的项目

[英]ASP.NET MVC 5 site - edit view not selecting item in DropDownListFor

I have an MVC 5 site...我有一个 MVC 5 站点...

A story can be optionally associated with a PlaceId.一个故事可以有选择地与一个 PlaceId 相关联。 A blank placeId is also valid.空白的 placeId 也是有效的。 A place can be associated with more than one story.一个地方可以与多个故事相关联。

Models楷模

public class Place
{
    public Guid Id { get; set; }
    public string PlaceName { get; set; }
}   

public class Story
{
    public Guid Id { get; set; }
    public Guid? PlaceId { get; set; }
    public string StoryName { get; set; }
}

I have several requirements.我有几个要求。

  • When editing the story I would like a drop down list of all places to be displayed - with the associated place (if any) - selected.编辑故事时,我希望显示所有地点的下拉列表 - 并选择相关地点(如果有)。
  • I want to use the strongly typed DropDownListFOR (as opposed to DropDownList).我想使用强类型 DropDownListFOR(而不是 DropDownList)。
  • I want to add a "No Associated Place" which will be selected if PlaceId is null (and should pass null back to the model).我想添加一个“No Associated Place”,如果 PlaceId 为 null(并且应该将 null 传回模型),它将被选中。
  • I want to add a css class = "form-control" to the DropDownListFor.我想在 DropDownListFor 中添加一个 css class = "form-control"。

Controller (Get)控制器(获取)

public ActionResult Edit(Guid Id)
{
    // Get Story
    var story = StoryRepo.SelectArticle(Id);

    // Put select list of all places in ViewBag
    ViewBag.PlaceId = new SelectList(PlaceRepo.SelectAll(), "Id", "PlaceName", new { Id = story.PlaceId });

    // Return view
    return View(story);
}

In View在视图中

@Html.DropDownListFor(x => x.PlaceId, (IEnumerable<SelectListItem>)ViewBag.PlaceId, "No Associated Place",
                                    new { @class = "form-control" })

This populates the dropdown list fine, and when you select an item the PlaceId is in the model bound to the controller.这会很好地填充下拉列表,当您选择一个项目时,PlaceId 位于绑定到控制器的模型中。 However - it does not select the existing PlaceId when the view loads.但是 - 它不会在视图加载时选择现有的 PlaceId。

Shouldn't the final parameter in the ViewBag.PlaceId line new { Id = story.PlaceId } - cause this selection? ViewBag.PlaceId 行中的最后一个参数不应该是new { Id = story.PlaceId } - 导致这个选择吗?

I can't find anything on this specific issue online - and can find little about how to bind a dropdown to a strongly typed edit view in the way I require.我在网上找不到关于这个特定问题的任何内容 - 并且几乎找不到关于如何以我需要的方式将下拉列表绑定到强类型编辑视图的信息。

How can I make it select the correct item?我怎样才能让它选择正确的项目? (also any improvements on how I am doing this also appreciated). (也对我如何做到这一点的任何改进也表示赞赏)。

Thanks.谢谢。

I think you need to change the following code我认为您需要更改以下代码

   // Put select list of all places in ViewBag
    ViewBag.PlaceId = new SelectList(PlaceRepo.SelectAll(), "Id",
                             "PlaceName", new { Id = story.PlaceId });

to become成为

   // Put select list of all places in ViewBag
    ViewBag.PlaceId = new SelectList(PlaceRepo.SelectAll(), "Id", 
                             "PlaceName", story.PlaceId);

Definition of the SelectedList from msdn msdn 中SelectedList 的定义

SelectList(IEnumerable, String, String, Object): Initializes a new instance of the SelectList class by using the specified items for the list, the data value field, the data text field, and a selected value. SelectList(IEnumerable, String, String, Object):使用列表的指定项、数据值字段、数据文本字段和选定值初始化 SelectList 类的新实例。

here a working demo这是一个工作演示

updated demo更新演示

Hope this will help you希望能帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM