asp.net-mvc 使用带有剃刀的 html 选择框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13884293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a html select box with razor
提问by Tk Breen
I have a html selector, and I want to use the selected value for my "model => model.type" in my form. Is there a way to set the value in my @Html.EditorFor(model => model.type)to the value of the selector?
我有一个 html 选择器,我想在我的表单中为我的“model => model.type”使用选定的值。有没有办法将 my@Html.EditorFor(model => model.type)中的值设置为选择器的值?
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Bet</legend>
<div class="editor-label">
@Html.LabelFor(model => model.type)
</div>
<div class="editor-field">
<select id ="type">
<option value="Football">Football</option>
<option value="Rugby">Rugby</option>
<option value="Horse Racing">Horse Racing</option>
</select>
@Html.EditorFor(model => model.type)
@Html.ValidationMessageFor(model => model.type)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
回答by andres descalzo
You can try with this options:
您可以尝试使用以下选项:
Model:
模型:
public string Type { get; set; }
public IEnumerable<SelectListItem> TypeList
{
get
{
return new List<SelectListItem>
{
new SelectListItem { Text = "Football", Value = "Football"},
new SelectListItem { Text = "Rugby", Value = "Rugby"},
new SelectListItem { Text = "Horse Racing", Value = "Horse Racing"}
};
}
}
HTML (Razor):
HTML(剃刀):
@Html.DropDownListFor(model => model.Type, Model.TypeList)
OR
或者
HTML (Razor):
HTML(剃刀):
@Html.DropDownListFor(model => model.Type, new SelectList(new string[] {"Football", "Rugby", "Horse Racing"}, Model.Type))
回答by mehdirahimi
@andresdescalzo's solution (last one) works with a minor change:
@andresdescalzo 的解决方案(最后一个)有一个小改动:
@Html.DropDownListFor(model => model.Type, new SelectList(new string[] {"Football", "Rugby", "Horse Racing"}, "Rugby"), htmlAttributes: new { @class = "form-control" })
First:Add a selected item for dropdown list (e.g. "Rugby")
Second:remove last Model.Typeand add htmlAttributes
PS: SelectedList open parentheses closed after selected item of list (here is "Rugby")
第一:为下拉列表添加所选项目(例如“Rugby”)
第二:删除最后一个Model.Type并添加htmlAttributes
PS:SelectedList 左括号在列表的所选项目之后关闭(这里是“ Rugby”)
回答by Diwas
The solution provided by @andresdescalzo works only when we pass the model from Controller to the view.
@andresdescalzo 提供的解决方案只有在我们将模型从 Controller 传递到视图时才有效。
public class SomeController : Controller
{
public ActionResult SomeAction()
{
return View(new SomeModelWithIEnumerationsSelectListItem())
}
}
回答by chakeda
An addition to the existing answers: If you get the exception "object reference not set to an instance of an object" when implementing @andresdescalzo's solution, you either need to:
现有答案的补充:如果在实现@andresdescalzo 的解决方案时出现异常“对象引用未设置为对象的实例”,则需要:
- Return the model that you are working with as @diwas mentioned
- Instantiate a model on the razor page so you can call the method. This is helpful if you are using a ViewModel instead of a simple EF model.
- 如@diwas 所述,返回您正在使用的模型
- 在 razor 页面上实例化一个模型,以便您可以调用该方法。如果您使用的是 ViewModel 而不是简单的 EF 模型,这会很有帮助。
The second can be done like this:
第二个可以这样做:
@{ var tempModel = new YourNameSpace.Models.YourModel(); }
@Html.DropDownListFor(model => model.Type, tempModel.TypeList, Model.Type))
回答by sapana
This is the Razor View
这是剃刀视图
<div class="editor-field col-md-3">
@Html.DropDownListFor(model => model.Servers,
Model.AvailableServers,new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.SqlServerName)
</div>
Dynamic DropDown from controller
从控制器动态下拉
List<SelectListItem> ServersAvailable = new List<SelectListItem>();
bool IdSelected = false;
ServersAvailable.Add(new SelectListItem { Text = "......Select your server name......", Value = "" });
for (int i = 0; i < dt.Rows.Count; i++)
{
ServersAvailable.Add(new SelectListItem
{
Text = dt.Rows[i].ItemArray[0].ToString(),
Value = dt.Rows[i].ItemArray[0].ToString(),
Selected = IdSelected
});
}

