Asp.net mvc 4 选定值的DropDownList不适用于列表中的属性

Asp.net mvc 4 选定值的DropDownList不适用于列表中的属性,asp.net-mvc-4,model,get,selecteditem,html.dropdownlistfor,Asp.net Mvc 4,Model,Get,Selecteditem,Html.dropdownlistfor,在ASP MVC 4中,我无法使用DropDownList for设置DropDownList的默认选定值 当我想要预选的属性在列表中时,它在默认情况下不会像预期的那样被选中 以下是我的代码,简化为重要内容: 型号: public class Renter { ... public virtual List<RentLine> RentLines { get; set; } ... public Renter() { ...

在ASP MVC 4中,我无法使用DropDownList for设置DropDownList的默认选定值

当我想要预选的属性在列表中时,它在默认情况下不会像预期的那样被选中

以下是我的代码,简化为重要内容:

型号:

public class Renter
{
    ...
    public virtual List<RentLine> RentLines { get; set; }
    ...

    public Renter()
    {
        ...
        RentLines = new List<RentLine>();
        ...
    }
}

public class RentLine
{
    ...
    public Guid RenterId { get; set; }
    public virtual Renter Renter { get; set; }  
    public Guid GarageId { get; set; }
    public virtual Garage Garage { get; set; }
    ...

    public RentLine()
    {
        //
        // Testcase for default value in dropdownlist
        //
        GarageId = new Guid("e926dcac-05ac-4fc1-92a2-014345e6e11c");
    }
}
视图模型:

public class AddRenterViewModel
{
    public Renter Renter { get; set; }

    public List<SelectListItem> RentableGarageItems { get; set; }

    ...

    public AddRenterViewModel()
    {
        Renter = new Renter();
        Renter.RentLines.Add(new RentLine());

        RentableGarageItems = new List<SelectListItem>();
        foreach (var garage in Singleton.Instance.Entities.GetRentableGarages().OrderBy(g => g.No))
        {
            RentableGarageItems.Add(new SelectListItem{Text = garage.No.ToString(), Value = garage.Id.ToString()});
        }
    }   
}
结果如下,未选择任何值:

Garage Nr.
<select id="Renter_RentLines_0__GarageId" name="Renter.RentLines[0].GarageId" data-val-required="Das Feld "GarageId" ist erforderlich." data-val="true">
<option value="225a761d-7d12-4e02-9d72-d54fb6934f05">1</option>
<option value="d28fb4fb-d65b-4674-90d6-a4ab9636be80">2</option>
<option value="e06b520b-e876-4870-b321-dc65f77967a7">3</option>
<option value="56832d98-aa5a-4e1a-af44-2d7e00f2bb3a">4</option>
<option value="551f1fa9-13a5-40ce-b795-50063e605027">5</option>
<option value="bace4b40-92ab-4757-9716-2a2878c2c9e8">6</option>
<option value="f9baf45b-96bf-463e-a234-a1522bfc9b9b">7</option>
<option value="91d1d866-6346-4ab0-b2aa-35b56eeaf2b0">8</option>
<option value="62eb3727-eab2-4180-90cd-7dc59119f44e">9</option>
<option value="59768097-c772-4421-b907-a24397a106f5">10</option>
<option value="270acf6b-e4e3-41c3-86dd-4381a313848a">11</option>
<option value="923269cd-1ead-4346-90ce-6344acda5b1b">12</option>
<option value="981ee61e-408b-4b08-a7d3-26c909f04a9d">13</option>
<option value="f1c5043c-fe41-4a8e-8c58-e88363885de4">14</option>
<option value="e926dcac-05ac-4fc1-92a2-014345e6e11c">15</option>
<option value="5577dc00-7cfa-4bcd-99ad-3f356e4325ae">16</option>
</select>
<input id="Renter_RentLines_0__GarageId" type="text" value="e926dcac-05ac-4fc1-92a2-014345e6e11c" name="Renter.RentLines[0].GarageId">

在SelectedListItem创建中缺少Selected属性。i、 e所选=(garage.Id==2)

var entities=新列表()
{
new Guarage(){No=1,Id=1},
new Guarage(){No=2,Id=2},
新担保(){No=3,Id=3}
};
foreach(实体中的var车库)
{
var s=新的SelectListItem
{
Text=garage.No.ToString(),
Value=garage.Id.ToString(),
所选=(garage.Id==2)
};
可出租车库项目。添加(s);
}
默认情况下,这将在下拉列表中选择2

经过一些研究,我找到了一个解决方案

这项工作:

@Html.DropDownListFor(m => m.Renter.RentLines[0].GarageId,
  new SelectList(Model.RentableGarageItems, "Value", "Text", Model.Renter.RentLines[0].GarageId))

内联Stephen Muecke对

不幸的是,在循环中呈现控件时,
@Html.DropDownListFor()
的行为与其他帮助程序略有不同。这以前在CodePlex上被报告为一个问题(不确定这是一个bug还是一个限制)

有两个选项可以解决此问题,以确保根据模型特性选择正确的选项


选项1-使用
编辑器或模板
为集合中的类型创建自定义
EditorTemplate
。在
/Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml
中创建分部文件(注意名称必须与类型名称匹配

@model yoursassembly.AggregationLevelConfiguration
@(m=>m.HelperCodeType,(SelectList)ViewData[“CodeTypeItems”])
..//AggregationLevelConfiguration的其他属性
然后在主视图中,将
选择列表
传递给
编辑器或模板
作为
附加视图数据

@使用(Html.BeginForm())
{
...
@EditorFor(m=>m.Configurations,新的{CodeTypeItems=Model.CodeTypeItems})
...

选项2-在每次迭代中生成一个新的
SelectList
,并设置
selectedValue
在此选项中,您的属性
CodeTypeItems
应该是
IEnumerable
,而不是
SelectList
(或者只是将
codeTypes
设置为公共属性)。然后在主视图中

@Html.DropDownListFor(m=>m.Configurations[0]。HelperCodeType,新选择列表(Model.CodeTypeItems,“Id”,“Name”,Model.Configurations[0]。HelperCodeType)
旁注:无需使用
新的{id=“Configurations[0].HelperCodeType”
-方法已生成
id
属性

注意:还要再次检查您是否存在以下问题中所述的其他与列表无关的绑定问题:


下面的答案可以解决您的问题吗?谢谢您的回复。这是可行的,但它必须能够在不设置SelectListItems列表中的选定值的情况下解决此问题,原因有两个:1.我想使用相同的列表,而不是具有不同预选值的DropDownList。2.在我想从属性“更深层”中进行预选之前,它工作得非常完美在我的模型对象中。请查看我上面更新的帖子。(无法作为评论发送,因为它太长了)。在这里,它可以工作,而不设置SelectListItems列表中的select值。这对我有效
Garage Nr.
<select id="Renter_RentLines_0__GarageId" name="Renter.RentLines[0].GarageId" data-val-required="Das Feld "GarageId" ist erforderlich." data-val="true">
<option value="225a761d-7d12-4e02-9d72-d54fb6934f05">1</option>
<option value="d28fb4fb-d65b-4674-90d6-a4ab9636be80">2</option>
<option value="e06b520b-e876-4870-b321-dc65f77967a7">3</option>
<option value="56832d98-aa5a-4e1a-af44-2d7e00f2bb3a">4</option>
<option value="551f1fa9-13a5-40ce-b795-50063e605027">5</option>
<option value="bace4b40-92ab-4757-9716-2a2878c2c9e8">6</option>
<option value="f9baf45b-96bf-463e-a234-a1522bfc9b9b">7</option>
<option value="91d1d866-6346-4ab0-b2aa-35b56eeaf2b0">8</option>
<option value="62eb3727-eab2-4180-90cd-7dc59119f44e">9</option>
<option value="59768097-c772-4421-b907-a24397a106f5">10</option>
<option value="270acf6b-e4e3-41c3-86dd-4381a313848a">11</option>
<option value="923269cd-1ead-4346-90ce-6344acda5b1b">12</option>
<option value="981ee61e-408b-4b08-a7d3-26c909f04a9d">13</option>
<option value="f1c5043c-fe41-4a8e-8c58-e88363885de4">14</option>
<option value="e926dcac-05ac-4fc1-92a2-014345e6e11c">15</option>
<option value="5577dc00-7cfa-4bcd-99ad-3f356e4325ae">16</option>
</select>
<input id="Renter_RentLines_0__GarageId" type="text" value="e926dcac-05ac-4fc1-92a2-014345e6e11c" name="Renter.RentLines[0].GarageId">
public class Renter
{
    ...
    public Salutation Salutation { get; set; }
    ...

    public Renter()
    {
        ...
        Salutation = Salutation.Male;
        ...
    }
}

public ActionResult Hinzufuegen()
{
    var model = new AddRenterViewModel();
    return View(model);
}

public class AddRenterViewModel
{
    public Renter Renter { get; set; }

    public List&lt;SelectListItem&gt; SalutationItems { get; set; }

    ...

    public AddRenterViewModel()
    {
        Renter = new Renter();
        Renter.RentLines.Add(new RentLine());

        SalutationItems = new List&lt;SelectListItem&gt;();
        foreach (Salutation salutation in Enum.GetValues(typeof(Salutation)))
        {
           SalutationItems.Add(new SelectListItem { Text = NameEnum.GetName(salutation), Value = salutation.ToString() });
        }
    }   
}

@Html.DropDownListFor(m =&gt; m.Renter.Salutation, Model.SalutationItems)

&lt;select id="Renter_Salutation" name="Renter.Salutation" data-val-required="Das Feld "Salutation" ist erforderlich." data-val="true"&gt;
&lt;option value="Company"&gt;Firma&lt;/option&gt;
&lt;option value="Male" selected="selected"&gt;Männlich&lt;/option&gt;
&lt;option value="Female"&gt;Weiblich&lt;/option&gt;
&lt;/select&gt;
        var entities = new List<Guarage>()
        {
            new Guarage() {No = 1, Id = 1},
            new Guarage() {No = 2, Id = 2},
            new Guarage() {No = 3, Id = 3}
        };

        foreach (var garage in entities)
        {
            var s = new SelectListItem
            {
                Text = garage.No.ToString(),
                Value = garage.Id.ToString(),
                Selected = (garage.Id == 2)
            };
            RentableGarageItems.Add(s);
        }
@Html.DropDownListFor(m => m.Renter.RentLines[0].GarageId,
  new SelectList(Model.RentableGarageItems, "Value", "Text", Model.Renter.RentLines[0].GarageId))