Html.EnumDropDownListFor根据枚举变量的值设置选定的列表项 [英] Html.EnumDropDownListFor Set selected listitem based on enum variable's value
问题描述
这可能是重复的,所以我已经指出了我从该站点上的阅读可以让我取得一些进步...
This may be a duplicate, so I have indicated where my reading from this site has allowed me some progress...
我有一个定义如下的模型:
I have a model defined as follows:
public enum RequestType
{
[Display(Name = "Lovely Cold Beer")]
Beer = 0,
[Display(Name = "Warm Tea")]
Tea = 1,
[Display(Name = "Milky Coffee")]
Coffee= 2
}
基于URL,我有一个变量,该变量将用于自动选择适当的列表项,例如
Based on the URL, I have a variable that will be used to automatically select the appropriate list item, e.g.
http://example.com/Request/Tea
将在控制器中执行此操作...
will do this in the controller...
ViewBag.RequestType = RequestType.Tea.ToString();
return View("Index");
在我看来,我有一个变量可以读回该值,然后显示适当的内容:
In my view I have a variable to read this value back, which then displays appropriate content:
if (ViewBag.RequestType != null)
{
reqType = Enum.Parse(typeof(RequestType), ViewBag.RequestType);
}
在此视图中,我使用以下命令创建一个下拉列表:
In this view I create a drop down list using:
@Html.EnumDropDownListFor(model => model.RequestType, htmlAttributes: new { @onchange = "YadaYada();" })
这将使用为每个Enum
定义的Display Name
值呈现列表.我需要的是在呈现页面时自动选择合适的列表项,该列表项与reqType
的值匹配.
This renders the list using the Display Name
values defined for each Enum
. What I need is to automatically select the appropriate list item when the page is rendered, that matches the value of reqType
.
从我的研究中,我可以像这样传递变量:
From my research I see that I can pass in the variable like so:
@Html.EnumDropDownListFor(model => model.RequestType, reqType.ToString(), htmlAttributes: new { @onchange = "YadaYada();" })
但这会创建一个新列表项,其中包含枚举值而不是显示名称,例如
But this creates a new list item containing the enum value and not the display name, e.g.
Tea <-- This should not be created, instead 'Warm Tea' should be selected
Lovely Cold Beer
Warm Tea
Milky Coffee
由于我是MVC的新手,所以我的整个方法可能是错误的,但是欢迎提出建议以解决此问题!我不明白为什么在控制器中,对枚举值使用ToString
会产生与在EnumDropDownListFor
方法中执行相同操作的结果不同的结果.
My entire approach may be wrong as I'm new to MVC, but I'd welcome advice to fix it please! I don't understand why in the controller, using ToString
on the enum value creates a different outcome to doing the same in the EnumDropDownListFor
method.
推荐答案
您的EnumDropDownListFor()
方法的第二个参数(reqType.ToString()
)使用的是
The second parameter (reqType.ToString()
) of your EnumDropDownListFor()
method is using the overload which adds an optionLabel
(an option with a null
value used for validation). It does not set the value of the selected option.
MVC的模型绑定功能通过绑定到属性来工作,并且由于RequestType
的默认值为"Beer"
,因此将选择该选项.
Model binding features of MVC work by binding to your property and since the default value of your RequestType
is "Beer"
, then that option will be selected.
例如,您需要在将模型传递到视图之前设置模型中属性的值(假设您具有/Request/{request}
的特定路线)
You need to set the value of the property in the model before you pass the model to the view, for example (assumes you have a specific route for /Request/{request}
)
public ActionResult Request(RequestType request)
{
var model = new MyModel
{
RequestType = request
};
return View(model);
}
这篇关于Html.EnumDropDownListFor根据枚举变量的值设置选定的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!