DropDownListFor不显示/迁延性空的首选名单中 [英] DropDownListFor Not Displaying/Persisting Null for First Choice in List

查看:107
本文介绍了DropDownListFor不显示/迁延性空的首选名单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过SharpArch一个MVC应用程序。在视图我有:

I have an MVC app via SharpArch. In the view I have:

 Html.DropDownListFor(x => x.Location, 
     new SelectList(Model.PossibleLocations, "Id", "Address"), 
     "-- No Location --")

我有2个问题。


  1. 下拉是没有得到当视图被绑定到模型更新。

  2. 的选择被正确地坚持除非我尝试顶没有位置。

我能够通过改变x.Location到x.Location.Id照顾的第一个点,但随后我有其他的问题。

I was able to take care of the first point by changing x.Location to x.Location.Id but then I had other issues.

我可以找到大量的对DropDownList的例子,但在保存空表示没有。

I can find plenty of examples for DropDownList, but none in which saving a null is shown.

任何帮助是AP preciated。

Any help is appreciated.

感谢

更新:

我刚刚升级ReSharper的(一个小更新)和我收到提示来填补它DropDownListFor。为什么会有所作为?这是工作够使,现在它根本不工作。

I just upgraded Resharper (a minor update) and am getting prompted to fill it DropDownListFor. Why would that make a difference? It was working enough to bind and now it doesn't work at all.

推荐答案

您可以先插入一个空 SelectListItem PossibleLocations 收集传递给查看前,并检查该空值(0为实例)当窗体发布

You could always insert the "empty" SelectListItem into your PossibleLocations collection before passing to the View and check for that "empty" value (0 for instance) when the form is posted

model.PossibleLocations.Insert(0, 
    new SelectListItem() { Text = "-- No Location --", Value = "0" };

这篇关于DropDownListFor不显示/迁延性空的首选名单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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