在@html.dropdownlistfor中设置选定的值

时间:2013-07-30 05:06:11

标签: asp.net-mvc-3

我在视图中有以下代码

 <td>@Html.DropDownListFor(e => e[0].Reason, new List<SelectListItem>
  (
   new[]
    {
       new SelectListItem { Text = " -Select- ", Value = "-Select-" },
       new SelectListItem { Text = " Price ", Value = "Price" },
       new SelectListItem { Text = " 3P ", Value = "3P" },
       new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
       new SelectListItem { Text = " Billing ", Value = "Billing" },
       new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
       new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
       new SelectListItem { Text = " Quality ", Value = "Quality" },
       new SelectListItem { Text = " Service ", Value = "Service" },
       new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
       new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
       new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
       new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
       new SelectListItem { Text = " BIC ", Value = "BIC" },
       new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }

        }
        ))</td>

每次页面加载选定值时都会发生变化

您能否告诉我如何设置所选值。

先谢谢。

5 个答案:

答案 0 :(得分:4)

将您的列表添加到ViewBag中,类型为IEnumerable-SelectListItem。 然后使用这部分代码。

@Html.DropDownListFor(e => e[0].Reason, new SelectList(ViewBag.YourSelectList, "Value", "Text","YourSelectedValue")

答案 1 :(得分:0)

您可以使用模型对象传递选定的值。从action方法中,您可以按如下方式返回模型对象

model obj=new model();
obj.Reason="Price";
return view(obj)

在视图中,加载自身时选择值为"Price"的项目

或者您可以使用jquery

设置所选值
$(document).ready(function(){
   $("#Reason").val("Price");
});

答案 2 :(得分:0)

如果要在视图中设置选定值:

@Html.DropDownListFor( e => e[0].Reason, new SelectList( new List<SelectListItem>(){
   new SelectListItem { Text = " -Select- ", Value = "-Select-" },
   new SelectListItem { Text = " Price ", Value = "Price" },
   new SelectListItem { Text = " 3P ", Value = "3P" },
   new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
   new SelectListItem { Text = " Billing ", Value = "Billing" },
   new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
   new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
   new SelectListItem { Text = " Quality ", Value = "Quality" },
   new SelectListItem { Text = " Service ", Value = "Service" },
   new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
   new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
   new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
   new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
   new SelectListItem { Text = " BIC ", Value = "BIC" },
   new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }
    } , "Value", "Text", e[0].Reason ) )

要在控制器中设置选定值,请选中答案:https://stackoverflow.com/a/17184481/1643075

答案 3 :(得分:0)

好的,veena,

我留下了一条评论,建议如果是我,我会怎么做。但是,您正在寻找基于您当前实现的答案,所以现在就是我对此的看法:

<td>
    @{ var reasonList = new List<SelectListItem>(new[] {
        new SelectListItem { 
            Text=" -Select-",
            Value="-Select- "
        },
        new SelectListItem { 
            Selected = Model[0].Reason.Equals("Price"), 
            Text=" Price",
            Value="Price"
        },
        new SelectListItem {
            Selected = Model[0].Reason.Equals("3P"),
            Text=" 3P",
            Value="3P"
        }
        // etc etc...
      });
    }
    @Html.DropDownListFor(m => m[0].Reason, reasonList)
</td>

但是,我强烈建议您将此逻辑移到控制器/服务代码中,因为当包含此类代码时,视图确实变得乱七八糟。我还强烈质疑以你的方式检查索引元素的基本原理。为了简洁,我假设你已经省略了索引[1],[2]等。如果是这种情况,那么你应该使用foreach循环并在代码中按顺序遍历元素。下面是一个非常快速的例子(语法可能稍微不正确,因为我通过ipad发送,因此未经测试):

@foreach(var item in Model) {
   @: <td>
   var reasonList = new List<SelectListItem>(new[] {
        new SelectListItem { 
            Text=" -Select-",
            Value="-Select- "
        },
        new SelectListItem { 
            Selected = item.Reason.Equals("Price"), 
            Text=" Price",
            Value="Price"
        },
        new SelectListItem {
            Selected = item.Reason.Equals("3P"),
            Text=" 3P",
            Value="3P"
        }
        // etc etc...
      });
    @Html.DropDownListFor(m => item.Reason, reasonList)
    @: </td>
}

答案 4 :(得分:0)

解决方案是将所选值放入SelectList对象的第四个参数中。

List<string> TheValuesYouWantToPresent = new List<string> { "one", "two", "three" };
string SelectedValue = "two";
List<SelectListItem> TheSelectListItems = TheValuesYouWantToPresent.Select(x => new SelectListItem { Text = x, Value = x}).ToList();
ViewBag.MyDropDownListDataWithASelectedItem = new SelectList(TheSelectListItems, "Text", "Value", SelectedValue);

然后在你的MVC视图中,你这样做:

@Html.DropDownListFor(model => model.DataBaseFieldToPopulate, ViewBag.MyDropDownListDataWithASelectedItem, "Invitation to select something from the list", new { @class = "a_CSS_class_name_of_your_choosing" })