简体   繁体   English

ASP.NET selectlistitem值int

[英]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.

相关问题 ASP.NET MVC:所选值不适用于列表<selectlistitem></selectlistitem> - ASP.NET MVC : selected value not working on a List<SelectListItem> asp.net mvc 5 SelectListItem有条件 - asp.net mvc 5 SelectListItem with condition 在 ASP.NET Core 中提交表单后如何保持 SelectListItem 值被选中? - How to keep SelectListItem value selected after submitting form in ASP.NET Core? 在 ASP.NET Core MVC 中如何插入返回 List 的方法的返回值<selectlistitem>在 Session</selectlistitem> - In ASP.NET Core MVC how insert the return value of a method that returns List<SelectListItem> in a Session ASP.NET 中的 MVC - 使用 IEnumerable 显示值而不是 Id<selectlistitem></selectlistitem> - MVC in ASP.NET - Displaying a value instead of Id using IEnumerable<SelectListItem> IEnumerable <SelectListItem> ASP.NET MVC 5中的简单成员资格错误 - IEnumerable<SelectListItem> error in Simple Membership in ASP.NET MVC 5 MVC asp.net否IEnumerable类型的ViewData项<SelectListItem> - MVC asp.net No ViewData item of type IEnumerable<SelectListItem> 具有下拉列表的Asp.Net MVC和SelectListItem协助 - Asp.Net MVC with Drop Down List, and SelectListItem Assistance ASP.NET MVC - HTML 错误“没有 IEnumerable 类型的 ViewData<selectlistitem> ”</selectlistitem> - ASP.NET MVC - HTML Error “No ViewData of Type IEnumerable<SelectListItem>” ASP.NET Core 6 MVC 中新 SelectListItem 中的条件逻辑 - Conditional logic in new SelectListItem in ASP.NET Core 6 MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM