1

我认为我有以下代码

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

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

你能告诉我如何设置选定的值。

提前致谢。

4

5 回答 5

4

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

@Html.DropDownListFor(e => e[0].Reason, new SelectList(ViewBag.YourSelectList, "Value", "Text","YourSelectedValue")
于 2014-03-24T13:54:09.173 回答
0

ok, veena,

I left a comment suggesting how I would do it if it were me. However, you are looking for an answer based on your current implementation, so here's my take on that for now:

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

However, I'd strongly suggest that you move this logic up into your controller/service code as the view really becomes littered when this kind of code is included. I'd also strongly question the rationale for examining the index elements in the way that you do. I'm presuming that you have ommitted indexes [1], [2] etc for brevity. If this is the case, then you should really use a foreach loop and go thro the elements sequentially in your code. Below is a very quick example of that too (the syntax may be slightly incorrect as i'm sending via ipad, so is untested):

@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>
}
于 2013-07-30T08:29:36.540 回答
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

于 2013-07-30T05:58:43.067 回答
0

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

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

在视图中,具有值的项目"Price"在加载自身时被选中

或者您可以使用 jquery 设置选定的值

$(document).ready(function(){
   $("#Reason").val("Price");
});
于 2013-07-30T05:13:17.730 回答
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" })
于 2017-05-14T22:33:48.870 回答