[英]ASP.NET selectlistitem value int
I have a dropdown like so...is it possible to have those Values as int so it would be 0 instead of "0" 我有一个这样的下拉列表...是否有可能将这些值设置为int,所以它将是0而不是“ 0”
@Html.DropDownListFor(model => model.guests, new List<SelectListItem>()
{
new SelectListItem{ Text="0", Value="0"},
new SelectListItem{ Text="1", Value="1"},
new SelectListItem{ Text="2", Value="2"}
}, "")
Think about what your code actually does. 考虑一下您的代码实际做什么。 All ASP.NET helpers like this do is create HTML, which will look something like:
像这样的所有ASP.NET助手都是创建HTML,其外观类似于:
<select id="guests" name="guests">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
It's up to you to parse this into an int if you need one, for example in javascript, but when you post this to an MVC action, assuming your guests
property is an int
, then your model will populate and post properly. 如果需要将其解析为一个int(例如在javascript中),则由您决定,但是当您将其发布到MVC操作时,假设
guests
属性为int
,则模型将正确填充并发布。
您应该在代码中将字符串转换为int。
int newInt = Convert.ToInt32(yourString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.