简体   繁体   中英

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"

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

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

 int newInt = Convert.ToInt32(yourString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM