如何为空组合框而不是0返回null?

时间:2018-04-18 23:37:01

标签: c# asp.net-mvc combobox

我有两个链接的组合,年和周。

我将一周的年份和空数据列表发送到视图

ViewBag.YearID = new SelectList(db.years, "YearID", "Name");
ViewBag.WeekID = new SelectList(db.week_list.Where(x => x.Name == ""), "WeekID", "Name");

在我有的视图中

@Html.DropDownList("YearID", null, "Seleccione Año", htmlAttributes: new { @class = "form-control" })
@Html.DropDownList("WeekID", null, htmlAttributes: new { @class = "form-control" })

现在,当开始年份有一条没有价值的帮助信息Select Year

<select class="form-control" id="YearID" name="YearID">
    <option value="">Seleccione Año</option>
    <option value="7">2016</option>
    <option value="8">2017</option>
    <option value="9">2018</option>
</select>

因为没有选择年份,我会在几周内传递一个空列表,结果如下:

<select class="form-control" id="WeekID" name="WeekID">
</select>

开始状态:

enter image description here

问题是当我点击Create按钮发送表单时。操作控制器为WeekID

获取错误的值
 YearID = null  -- as I espect
 WeekID = 0     -- dont know why. I expect null

然后验证显示Year是必需的,但是没有显示Week的任何内容,因为已经收到0

验证状态:

enter image description here

2 个答案:

答案 0 :(得分:1)

您需要将属性设为可为空并添加[Required]属性。

当您提交表单时,DefaultModelBinder会初始化您的模型,此时YearIDWeekID的值均为0(默认值为int

然后从请求中读取名称/值对。由于name/value YearIDnull DefaultModelBinderYearID = null尝试设置失败的ModelStateYearID错误是actualValue添加了0(该属性的attemptedValuenullWeekID<option>

由于name/value的下拉列表中不包含任何WeekID元素,因此请求中不会发送0对,因此不会尝试设置值和值0仍为int。由于ModelStateWeekID有效,因此DropDownList()没有Seleccione Año错误,这就是没有显示错误消息的原因。

此外,您使用public class YourVM { [Required(ErrorMessage = "...")] public int? SelectedYear { get; set; } // nullable [Required(ErrorMessage = "...")] public int? SelectedWeek { get; set; } // nullable public IEnumerable<SelectListItem> YearList { get; set; } public IEnumerable<SelectListItem> WeekList { get; set; } } 表示无论您在第一个下拉列表中选择什么,当您返回视图时,将选择第一个选项(@Html.DropDownListFor(m => m.SelectedYear, Model.YearList, "Seleccione Año", new { @class = "form-control" }) @Html.ValidatinMessageFor(m => m.SelectedYear) @Html.DropDownListFor(m => m.SelectedWeek, Model.WeekList, "Seleccione Año", new { @class = "form-control" }) @Html.ValidatinMessageFor(m => m.SelectedWeek) )(用户之前的用户)选中失败了)

您正在编辑数据,因此您应始终使用包含

的视图模型
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml

# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>

并在视图中强烈绑定到您的模型

mat-tab-group

答案 1 :(得分:0)

我知道这不是你问的答案,但我用来处理依赖控制问题的模式是在第一个有效之后才显示第二个。根据经验,这种模式给我带来了很多麻烦。