如何在 运行 时间内将默认值设置为 MVC 中的 select 列表

How to set default value to select list in MVC during run time

我有一个视图,它循环遍历模型并以可编辑模式显示详细信息。其中一个模型值来自 select 列表,如下所示

@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 提交时如果用户 selected Internal 它的值 1 将传递给控制器​​并且如果 Standard 它将是 2 这里的默认值将是 "--- Select Activity Code Type ---"

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

我是这样编码的

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

但它没有按预期工作,它给出的结果如下图

这里应该默认selected 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' does not contain a definition for 'EditorFor' and the best extension method overload 'System.Web.Mvc.Html.EditorExtensions.EditorFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, string, object)' has some invalid arguments

不幸的是,@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 作为 additionalViewData 传递给 EditorTemplate

@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 循环的每次迭代中创建一个新的 SelectList,您需要在其中设置 Selected 属性 of 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 ---")
}

您可以简单地通过使用 IEnumerable 模型类型列表来尝试这个

 @Html.DropDownList("ReceiverID", (IEnumerable<SelectListItem>)(m => m.activity_code_type), "--- Select ---", new { @class ="form-control" }) 

希望有用