I have a form in ASP MVC (Razor) and in this form I have a Textboxes... and a <select>
and I want to transmit the selected option (value) of this select to the controller like the other information. The textboxes are transmitted correctly to the controller and it's in the database.
我在ASP MVC(Razor)中有一个表单,在这个表单中我有一个Textboxes ...和一个
I have a field in the database called NumberParticipant and I want to transmit i tin the database thanks to my Create controller :
我在数据库中有一个名为NumberParticipant的字段,我希望通过我的Create控制器传输数据库:
My code :
我的代码:
@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(model => model.LocalNumber, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LocalNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LocalNumber)
</div>
</div>
<div class="col-md-10">
<select class="form-control" id="numberParticipant">
<option value=""></option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<input type="submit" value="Create" class="btn btn-default" />
}
1 个解决方案
#1
5
Using the MVC DropDownList helper would save you from writing out all the html. Here's an example that sets the option values in the view.
使用MVC DropDownList帮助程序可以避免写出所有的html。这是一个在视图中设置选项值的示例。
<div class="col-md-10">
@Html.DropDownListFor(model => model.NumberParticipant,
new SelectList(new []
{ new {value="",text=""},
new {value="2",text="2"},
new {value="3",text="3"},
new {value="4",text="4"},
new {value="5",text="5"}
},"value","text"), new { htmlAttributes = new { @class = "form-control" }})
</div>
A best practice would be to add a property of type SelectList to your model class and set the possible options there. You could then reference the new property in the helper like this:
最佳做法是将SelectList类型的属性添加到模型类,并在那里设置可能的选项。然后,您可以像这样在助手中引用新属性:
<div class="col-md-10">
@Html.DropDownListFor(model => model.NumberParticipant,
Model.MySelectList, new { htmlAttributes = new { @class = "form-control" }})
</div>
#1
5
Using the MVC DropDownList helper would save you from writing out all the html. Here's an example that sets the option values in the view.
使用MVC DropDownList帮助程序可以避免写出所有的html。这是一个在视图中设置选项值的示例。
<div class="col-md-10">
@Html.DropDownListFor(model => model.NumberParticipant,
new SelectList(new []
{ new {value="",text=""},
new {value="2",text="2"},
new {value="3",text="3"},
new {value="4",text="4"},
new {value="5",text="5"}
},"value","text"), new { htmlAttributes = new { @class = "form-control" }})
</div>
A best practice would be to add a property of type SelectList to your model class and set the possible options there. You could then reference the new property in the helper like this:
最佳做法是将SelectList类型的属性添加到模型类,并在那里设置可能的选项。然后,您可以像这样在助手中引用新属性:
<div class="col-md-10">
@Html.DropDownListFor(model => model.NumberParticipant,
Model.MySelectList, new { htmlAttributes = new { @class = "form-control" }})
</div>