简体   繁体   English

在视图中显示之前检查ListBoxFor selectedValues是否为null?

[英]Check if ListBoxFor selectedValues is null before display in view?

I have a number of ListBoxFor elements on a form in edit mode. 在编辑模式下,表单上有许多ListBoxFor元素。 If there was data recorded in the field then the previously selected items are displaying correctly when the form opens. 如果字段中记录了数据,则打开表格时,先前选择的项目将正确显示。 If the field is empty though an error is thrown as the items parameter cannot be null. 如果字段为空,但会引发错误,因为items参数不能为null。 Is there a way to check in the view and if there is data to use the ListBoxFor with the four parameters but if there isn't to only use three parameters, leaving out the selected items? 有没有一种方法可以检查视图,是否有数据可以使用带有四个参数的ListBoxFor,但是如果不只使用三个参数,则不包括所选项?

This is how I'm declaring the ListBoxFor: 这就是我声明ListBoxFor的方式:

@Html.ListBoxFor(model => model.IfQualityPoor, new MultiSelectList(ViewBag.IfPoor, "Value", "Text", ViewBag.IfQualityPoorSelected), new { @class = "chosen", multiple = "multiple" })  

I'm using the ViewBag to pass the ICollection which holds the selected items as the controller then joins or splits the strings for binding to the model field. 我正在使用ViewBag传递ICollection,该ICollection包含选定的项,因为控制器然后将其加入或拆分字符串以绑定到模型字段。 The MultiSelectLists always prove problematic for me. MultiSelectLists总是对我有问题。

Your question isn't entirely clear, but you're making it way harder on yourself than it needs to be using ListBoxFor . 您的问题尚不完全清楚,但是您正在使自己更困难,而不是需要使用ListBoxFor All you need for either DropDownListFor or ListBoxFor is an IEnumerable<SelectListItem> . DropDownListForListBoxForIEnumerable<SelectListItem> Razor will take care of selecting any appropriate values based on the ModelState . Razor将根据ModelState选择适当的值。

So, assuming ViewBag.IfPoor is IEnumerable<SelectListItem> , all you need in your view is: 因此,假设ViewBag.IfPoorIEnumerable<SelectListItem> ,则您在视图中所需ViewBag.IfPoor就是:

@Html.ListBoxFor(m => m.IfQualityPoor, (IEnumerable<SelectListItem>)ViewBag.IfPoor, new { @class = "chosen" })

The correct options will be marked as selected based on the value of IfQualityPoor on your model, as they should be. 正确的选项将根据模型上的IfQualityPoor值标记为已selected Also, it's unnecessary to pass multiple = "multiple" in in your htmlAttributes param, as you get that just by using ListBoxFor rather than DropDownListFor . 另外,也没有必要在htmlAttributes参数中传递multiple = "multiple" ,因为仅使用ListBoxFor而不是DropDownListFor获得该参数。

It's even better if you use a view model and then add your options as a property. 如果使用视图模型,然后将选项添加为属性,那就更好了。 Then, you don't have to worry about casting in the view, which is always a good way to introduce runtime exceptions. 然后,您不必担心在视图中进行强制转换,这始终是引入运行时异常的好方法。 For example: 例如:

public class FooViewModel
{
    ...

    public IEnumerable<SelectListItem> IfQualityPoorOptions { get; set; }
}

Then, you set this in your action, before returning the view (instead of setting ViewBag ). 然后,在返回视图之前(而不是设置ViewBag ),在操作中对此进行设置。 Finally, in your view: 最后,您认为:

@Html.ListBoxFor(m => m.IfQualityPoor, Model.IfQualityPoorOptions, new { @class = "chosen" })

Much simpler, and you'll never have any issues doing it that way. 这样简单得多,而且这样做绝对不会有任何问题。

UPDATE (based on comment) 更新(基于注释)

The best way to handle flattening a list into a string for database storage is to use a special property for that, and then custom getter and setter to map to/from. 处理将列表展平为字符串以存储数据库的最佳方法是为此使用一个特殊的属性,然后自定义getter和setter映射到/来自。 For example: 例如:

public string IfQualityPoor
{
    get { return IfQualityPoorList != null ? String.Join(",", IfQualityPoorList) : null; }
    set { IfQualityPoorList = !String.IsNullOrWhiteSpace(value) ? value.Split(',').ToList() : null; }
}

[NotMapped]
public List<string> IfQualityPoorList { get; set; }

Then, you post to/interact with IfQualityPoorList , and the correct string will be set in the database automatically when you save. 然后,将其发布到IfQualityPoorList或与IfQualityPoorList进行交互,并在保存时自动在数据库中设置正确的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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