2015-07-02 18 views
2

我有查看,其中它在模型中循环并以可编辑模式显示细节。其中一个模型值是从选择列表类似下面如何设置默认值以在运行时在MVC中选择列表

@if (Model != null) 
    { 
    for (int i = 0; i < Model.provider_service_dtls.Count; i++) 
    { 
    <tr> 
    <td> @Html.DropDownListFor(m => m.provider_service_dtls[i].activity_code_type, 
(SelectList)@ViewBag.activity_code_type, "--- Select Activity Code Type ---", new { @class = "m-wrap" })</td> 
    <td>@Html.TextBoxFor(m => m.provider_service_dtls[i].activity_name)</td>  
    </tr> 

    } 
    } 

这里ViewBag.activity_code_type包含在提交时Internal & Standard的值,如果用户选择Internal它的价值1将传递给控制器​​,如果Standard这将是2这里默认值将是"--- Select Activity Code Type ---"

enter image description here

现在,当我在编辑模式下打开相同的请求如果provider_service_dtls[i].activity_code_type模型值是1如果是2,则选择列表应该默认选择为InternalStandard

我编写这样

@Html.DropDownListFor(m => m.provider_service_dtls[i].activity_code_type, 
(SelectList)@ViewBag.activity_code_type, Model.provider_service_dtls[i].activity_code_type) 

但预期它给结果如下图

enter image description here

这应该默认选中Internal它无法正常工作。做到这一点的变化是什么?

编辑

型号

public partial class provider_service_dtls 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public long service_id { get; set; } 

     public long preapproval_id { get; set; } 

     public string activity_code_type { get; set; } 
     public string activity_type { get; set; } 

     public string activity_type_name { get; set; } 


     public string activity_code { get; set; } 
     public string activity_name { get; set; } 
     public string internal_activity_code { get; set; } 
     public string internal_activity_name { get; set; } 


     [ForeignKey("preapproval_id"), InverseProperty("provider_service_dtls")] 
     public virtual provider_preapproval preapproval { get; set; } 


    } 

编辑模板

@model Provider.Models.provider_preapproval 
@Html.DropDownListFor(m => m.activity_code_type, (SelectList)ViewData["options"]) 

查看

在for循环我编写这样

@Html.EditorFor(m => m.provider_service_dtls, 
new { options = (SelectList)@ViewBag.activity_code_type }) 

我得到一个错误

'System.Web.Mvc.HtmlHelper' 并不 不包含 'EditorFor' 的定义和最好扩展方法 超负荷 'System.Web.Mvc.Html.EditorExtensions.EditorFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, 串,对象)' 具有一些无效参数

回答

2

不幸的是@Html.DropDownListFor()在循环中呈现控件时的行为与其他帮助程序有点不同。这已被报告为CodePlex上的问题(不知道它是一个错误还是一个限制)

为集合中的类型创建一个自定义EditorTemplate

/Views/Shared/EditorTemplates/provider_service_dtls.cshtml(注意名称必须的类型的名称相匹配)

@model yourAssembly.provider_service_dtls 

@Html.HiddenFor(m => m.service_id) 
@Html.DropDownListFor(m => m.activity_code_type, (SelectList)ViewData["options"], "--- Select Activity Code Type ---") 
.... // html helpers for other properties of provider_service_dtls 

,然后在主视图中,传递的SelectList到EditorTemplate作为additionalViewData

@model yourAssembly.provider_preapproval 

@using (Html.BeginForm()) 
{ 
    .... // html helpers for other properties of provider_preapproval 

    @Html.EditorFor(m => m.provider_service_dtls, new { options = (SelectList)@ViewBag.activity_code_type }) 
    ... 

EditorFor()方法接受IEnumerable<T>并将生成控件集合中的每个项目

编辑

另一种方法是在for循环,在这里你需要设置的SelectListSelected财产的每个迭代创建一个新的SelectList。这意味着你的ViewBag属性必须是IEnumerable<T>,不是SelectList,例如在控制器

ViewBag.ActivityCodeTypeList = new[] 
{ 
    new { ID = 1, Name = "Internal" }, 
    new { ID = 2, Name = "Standard" } 
} 

,并在视图

for (int i = 0; i < Model.provider_service_dtls.Count; i++) 
{ 
    @Html.DropDownListFor(m => m => m.provider_service_dtls[i].activity_code_type, 
    new SelectList(ViewBag.ActivityCodeTypeList, "ID", "Name", Model.provider_service_dtls[i].activity_code_type), 
    "--- Select Activity Code Type ---") 
} 
+0

,所以我不能用这个forloop? – Sachu

+0

那么还有另一种选择,但这意味着你需要在每次迭代中构建一个新的'SelectList'(效率不是很高)。如果你使用'ListBoxFor()',它也是唯一的方法,所以我建议你使用'EditorTemplate',这是一个更好的方法。更何况你现在有一个可重用的控件,所以在你的应用程序的其他任何地方你需要为这个类型生成控件,你可以使用'@ Html.EditorFor()' –

+0

也可以使用'@ Html.DropDownListFor()'在最后的代码片段中是错误的。第三个参数用于生成一个'optionLabel'(即''---选择活动代码类型---“'你以前使用过的'),这就是为什么你的第二个图像显示'1''作为第一个选项 –

0

只要您可以使用像

IEnumerable的型号类型列表试试这个
@Html.DropDownList("ReceiverID", (IEnumerable<SelectListItem>)(m => m.activity_code_type), "--- Select ---", new { @class ="form-control" }) 

希望它会工作