如何设置默认值在运行时的MVC选择列表: [英] How to set default value to select list in MVC during run time

查看:101
本文介绍了如何设置默认值在运行时的MVC选择列表:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为在它遍历模型,并显示可编辑模式的细节。其中一个模型的价值是从选择列表如下图所示。

  @if(型号!= NULL)
    {
    的for(int i = 0; I< Model.provider_service_dtls.Count;我++)
     {
    &所述; TR>
    &所述; TD> @ Html.DropDownListFor(M = GT; m.provider_service_dtls [I] .activity_ code_type,
 (的SelectList)@ ViewBag.activity_ code_type---选择活动code型---新{@class =M-包})LT; / TD>
    < TD> @ Html.TextBoxFor(M = GT; m.provider_service_dtls [I] .activity_name)LT; / TD>
    < / TR>    }
    }

下面的 ViewBag.activity_ code_type 包含的值内部和放大器;标准如果用户选择在提交时 内部其值 1 将传递,如果<到控制器和code>标准这将是 2 ,这里的默认值将是---选择活动code键入---

现在,当我打开编辑模式相同的请求如果 provider_service_dtls模型值[I] .activity_ code_type 1 选择列表应该是默认选择为内部标准如果是 2

I codeD这样的

  @ Html.DropDownListFor(M = GT; m.provider_service_dtls [I] .activity_ code_type,
 (的SelectList)@ ViewBag.activity_ code_type,Model.provider_service_dtls [I] .activity_ code_type)

但预期它给结果,如下图所示它不工作

这应该默认选中内部。什么是做达到同样的变化?

编辑

型号

 公共部分类provider_service_dtls
    {
        [键]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)
        众长的service_id {搞定;组; }        众长$ P $ {papproval_id获得;组; }        公共字符串activity_ $ C $ {c_type获得;组; }
        公共字符串ACTIVITY_TYPE {搞定;组; }        公共字符串activity_type_name {搞定;组; }
        公共字符串activity_ code {搞定;组; }
        公共字符串ACTIVITY_NAME {搞定;组; }
        公共字符串internal_activity_ code {搞定;组; }
        公共字符串internal_activity_name {搞定;组; }
        [ForeignKey的(preapproval_id),InverseProperty(provider_service_dtls)]
        公共虚拟provider_ preapproval $ P $ {papproval获得;组; }
    }

编辑模板

  @model Provider.Models.provider_ preapproval
@ Html.DropDownListFor(M = GT; m.activity_ code_type,(的SelectList)ViewData的[选项])

查看

在for循环I codeD这样的

  @ Html.EditorFor(M = GT; m.provider_service_dtls,
新{选项=(的SelectList)@ ViewBag.activity_ code_type})

我得到一个错误


  

System.Web.Mvc.HtmlHelper'呢
  不包含'EditorFor和最好的扩展方法的定义
  超载
  System.Web.Mvc.Html.EditorExtensions.EditorFor(System.Web.Mvc.HtmlHelper,
  System.Linq.Ex pressions.Ex pression>
  字符串对象)'具有一些无效参数



解决方案

不幸的是 @ Html.DropDownListFor()在一个循环中呈现控件时的表现有点不同于其他佣工。这一直是pviously报告为C $ CPLEX在$一个问题$ P $(不知道它的一个bug或只是一个限制)

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

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

  @model yourAssembly.provider_service_dtls@ Html.HiddenFor(M = GT; m.service_id)
@ Html.DropDownListFor(M = GT; m.activity_ code_type,(的SelectList)ViewData的[选项],---选择活动code型---)
.... // HTML辅助的provider_service_dtls的其他属性

,然后在主视图中,通过选择列表到EditorTemplate为additionalViewData

  @model yourAssembly.provider_ preapproval@using(Html.BeginForm())
{
  .... // HTML辅助对provider_ preapproval的其他属性  @ Html.EditorFor(M = GT; m.provider_service_dtls,新{选项=(的SelectList)@ ViewBag.activity_ code_type})
  ...

EditorFor()方法接受的IEnumerable&LT; T&GT; 键,将生成控件集合中的每个项目

修改

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

  ViewBag.Activity codeTypeList =新[]
{
  新{ID = 1,名称=内部},
  新{ID = 2,名称=标准}
}

和视图

 的for(int i = 0; I&LT; Model.provider_service_dtls.Count;我++)
{
  @ Html.DropDownListFor(M = GT; M =&GT; m.provider_service_dtls [I] .activity_ code_type,
    新的SelectList(ViewBag.Activity codeTypeList,ID,姓名,Model.provider_service_dtls [I] .activity_ code_type)
    ---选择活动code型---)
}

I have view in which it loop through the model and display the details in editable mode. One of the model value is from a select list like below

@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>

    }
    } 

Here the ViewBag.activity_code_type contain the values Internal & Standard when submitting if user selected Internal its value 1 will pass to controller and if Standard it will be 2 and here the default value will be "--- Select Activity Code Type ---"

Now when i open the same request in edit mode if the model value for provider_service_dtls[i].activity_code_type is 1 the select list should be default select as Internal and Standard if it is 2.

I coded like this

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

But it is not working as expected it is giving the result as below picture

Here it should default selected Internal. What is the change to do achieve the same?

Edited

Model

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; }


    }

Editor template

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

View

Inside the for loop i coded like this

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

I am getting an error

'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

解决方案

Unfortunately @Html.DropDownListFor() behaves a little differently than other helpers when rendering controls in a loop. This has been previously reported as an issue on CodePlex (not sure if its a bug or just a limitation)

Create a custom EditorTemplate for the type in the collection.

In /Views/Shared/EditorTemplates/provider_service_dtls.cshtml (note the name must match the name of the type)

@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

and then in the main view, pass the SelectList to the EditorTemplate as 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 })
  ...

The EditorFor() methods accepts IEnumerable<T> and will generate the controls for each item in the collection

Edit

An alternative is to create a new SelectList in each iteration of the for loop, where you need to set the Selected property of SelectList. This means that your ViewBag property must be IEnumerable<T>, not a SelectList, for example in the controller

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

and in the view

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 ---")
}

这篇关于如何设置默认值在运行时的MVC选择列表:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆