如何为空的组合框而不是0返回null? [英] How return null for an empty combo box instead of 0?

查看:63
本文介绍了如何为空的组合框而不是0返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two linked combo, Year and Week.

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

Im sending the list of year and empty data set for week to the View

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" })

启动时立即年份有帮助信息选择年份无值。

Right now when start Year have a help message Select Year without value.

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

因此,由于未选择年份,因此我将一个空列表传递了几周,结果如下:

So because not year has been selected I pass an empty list for weeks and result on this:

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

开始状态:

问题是当我点击创建按钮发送表格。操作控制器为WeekID获取错误的值

The problem is when I hit Create button to send the form. The action controller get the wrong value for WeekID

 YearID = null  -- as I espect
 WeekID = 0     -- dont know why. I expect null

然后需要验证显示Year,但对于Week不显示任何内容,因为已经收到0

Then the validation show Year is required, but doesnt show anything for Week because already received 0

验证状态:

推荐答案

您需要使该属性可为空并添加 [必需] 属性。

You need to make the property nullable and add the [Required] attribute.

提交表单时, DefaultModelBinder 会初始化您的模型, YearID WeekID 的值均为 0 int 的默认值)

When you submit your form, the DefaultModelBinder initializes your model, and at that point, the value of YearID and WeekID are both 0 (the default value for int)

然后从请求中读取名称/值对。因为对于 YearID ,有一个名称/值对,它为 null DefaultModelBinder 尝试设置 YearID = null 失败,并设置 ModelState <为 YearID 添加了/ code>错误(该属性的 actualValue 0 attemptedValue null

The name/value pairs are then read from the request. Because there is a name/value pair for YearID which is null, the DefaultModelBinder attempts to set YearID = null which fails, and a ModelState error is added for YearID (the actualValue of the property is 0 and the attemptedValue is null)

因为 WeekID 的下拉列表不包含任何< option> 元素,因此不包含名称/值对在请求中发送,因此没有尝试设置值,并且 WeekID 的值仍然是 0 。因为 0 int 有效,所以没有 ModelState WeekID 的错误,这就是为什么没有显示错误消息的原因。

Because the dropdownlist for WeekID does not contain any <option> elements, no name/value pair is sent in the request, so no attempt is made to set the value, and the value of WeekID remains a 0. Because 0 is valid for int, there is no ModelState error for WeekID, which is why no error message is displayed.

此外,您使用 DropDownList()意味着无论您在第一个下拉列表中选择什么,返回视图时,第一个选项( SeleccioneAño )将被选中(先前选择的用户将丢失)

In addition, your use of DropDownList() means that no matter what you select in the first dropdownlist, when you return the view, the first option (Seleccione Año) will be selected (what the user previously selected is lost)

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

You are editing data so you should always be using a view model which will include

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; }
}

并在视图中牢固绑定到模型

and in the view, strongly bind to your model

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

这篇关于如何为空的组合框而不是0返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆