简体   繁体   English

@ Html.DropDownList仅返回1个值

[英]@Html.DropDownList only returns 1 value

I am trying to learn MVC ASP.Net, a step in the tutorial advises writing the following code to return all items in the specified list to a dropdown allowing the user to select the required item. 我正在尝试学习MVC ASP.Net,本教程中的一个步骤建议编写以下代码以将指定列表中的所有项目返回到下拉列表,以允许用户选择所需的项目。

Basic Explanation Drop down list only shows the current item returned eg if the URL is details/2 only 'Pizza' is returned in the list rather than all items in the 'MenuGroups' list. 基本说明下拉列表仅显示当前返回的项目,例如,如果URL为details / 2,则列表中仅返回“ Pizza”,而不是“ MenuGroups”列表中的所有项目。 Basic Explanation 基本说明

Although the code below does work to an extent it does not work the way I am shown in the video, I have even gone as far as going to the website and copying his exact code but I still receive different results and I cannot work out why. 尽管下面的代码确实在某种程度上不能像视频中所显示的那样工作,但我什至走到了网站并复制了他的确切代码,但是我仍然收到不同的结果,我不知道为什么。

http://cop4834.pbworks.com/w/page/106053825/Menu%20Case%20Study%20Part%204%20-%20Generating%20a%20Javascript%20call%20from%20a%20Razor%20Object http://cop4834.pbworks.com/w/page/106053825/Menu%20Case%20Study%20Part%204%20-%20Generating%20a%20Javascript%20call%20from%20a%20Razor%20Object

As you can see (If you have time to watch the video the demonstration is at 3:49) all menu groups are returned, my code displays only whatever is passed in via the URL? 如您所见(如果您有时间观看视频,演示时间为3:49),则返回了所有菜单组,我的代码仅显示通过URL传递的内容?

    <dt>
     Select a Menu Group
    </dt>

    <dd>
       @Html.DropDownList("MenuGroup", new SelectList(Model.MenuGroups,
          "MenuGroupId", "MenuGroupTitle"),
          new { onchange = "selectMenuGroup()" })
    </dd>

'MenuGroups' is a virtual list created from the Model 'Menu' “ MenuGroups”是从“菜单”模型创建的虚拟列表

    public virtual List<MenuGroup> MenuGroups { get; set; }

The controller is as below: 控制器如下:

    public ActionResult FullDetails(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Menu menu = db.Menus.Find(id);
        if (menu == null)
        {
            return HttpNotFound();
        }
        return View(menu);
    }

Apologies if this is too long or missed any information, I have looked in to this for many hours before posting but I can only find people saying to use @Html.DropDownListFor instead but I already have a list and so would like to use the above method (and also find out my mistake). 抱歉,如果时间太长或错过了任何信息,我在发布前已经调查了好几个小时,但我只能找到说使用@ Html.DropDownListFor的人,但是我已经有了一个列表,因此想使用上面的列表方法(并找出我的错误)。

Thanks in advance for any advice. 在此先感谢您的任何建议。

First make sure that MenuGroups contains multiple items for your selected record. 首先,确保MenuGroups包含用于所选记录的多个项目。

Then what you can do is replace 那你可以做的就是更换

public ActionResult FullDetails(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Menu menu = db.Menus.Find(id);
    if (menu == null)
    {
        return HttpNotFound();
    }
    return View(menu);
}

with

public ActionResult FullDetails(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Menu menu = db.Menus.Include(r => r.MenuGroups).FirstOrDefault(r => r.Id == id);
    if (menu == null)
    {
        return HttpNotFound();
    }
    return View(menu);
}

Since MenuGroups is a navigation property, you can eager load it by specifying Include(r => r.MenuGroups) . 由于MenuGroups是导航属性,因此您可以通过指定Include(r => r.MenuGroups)来渴望加载它。 AFAIK Find only returns the entity not including the navigation properties and it is not usable together with Include . AFAIK Find仅返回不包含导航属性的实体,并且不能与Include一起使用。 The reason why EF uses the virtual keyword is to allow lazy loading on the property. EF之所以使用virtual关键字,是为了允许对该属性进行延迟加载。 See Lazy Loading vs Eager Loading for more info. 有关更多信息,请参见懒加载与急加载

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

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