DropDownlist 或 DropDownListFor Html helper 之间的区别 [英] Difference between DropDownlist or DropDownListFor Html helper

查看:12
本文介绍了DropDownlist 或 DropDownListFor Html helper 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到这两个助手之间区别的解释,这似乎很奇怪,所以我认为这是显而易见的,但我错过了.

It seems weird that I couldn't find an explanation of the difference between those two helpers, so I would assume that is something obvious but I missed.

基本上,我正在尝试使用以下简单模型来决定我应该使用哪个模型:

Basically I am trying to decide which one I should use for my case, with the following simple Model:

public class Booking
    {
        public int ID { get; set; }
        public Room Room { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public ICollection<Equipment> Equipments { get; set; }
        public string Who { get; set; }
    }

我想显示一个简单的 Room DropDownlist,用于添加和编辑预订记录.

and I want display a simple Room DropDownlist for Adding and Editing Booking record.

在谷歌搜索了很多之后,似乎我可能需要一个 DropDopwListFor,但不确定为什么以及如何?

After doing a lots of Google around, it seems that I probably need a DropDopwListFor, but not sure why and how?

推荐答案

举以下两个例子:

@Html.DropDownListFor(
    x => x.EquipmentId, 
    new SelectList(Model.Equipments, "Id", "Text")
)

和:

@Html.DropDownList(
    "EquipmentId", 
    new SelectList(Model.Equipments, "Id", "Text")
)

很明显,在第二个示例中,您将下拉列表绑定到的属性名称被硬编码为魔术字符串.这意味着,如果您决定重构您的模型并重命名此属性,您可能正在使用的工具支持将无法检测此更改并自动修改您在许多视图中硬编码的魔术字符串.所以你将不得不手动搜索 &替换所有使用此弱类型助手的地方.

It is obvious that with the second example the name of the property you are binding the dropdown to is hardcoded as a magic string. This means that if you decide to refactor your model and rename this property Tooling support that you might be using has no way of detecting this change and automatically modifying the magic string you hardcoded in potentially many views. So you will have to manually search & replace everywhere this weakly typed helper is used.

另一方面,在第一个示例中,我们使用了一个与给定模型属性相关联的强类型 lambda 表达式,因此如果您决定重构代码,工具能够在任何使用它的地方自动重命名它.此外,如果您决定预编译您的视图,您将立即收到指向需要修复的视图的编译器时间错误.在第二个示例中,您(理想情况下)或您网站的用户(最坏的情况)在访问此特定视图时会遇到运行时错误.

With the first example on the other hand we are using a strongly typed lambda expression tied to the given model property so tools are able to automatically rename it everywhere it is used if you decide to refactor your code. Also if you decide to precompile your views you will get a compiler time error immediately pointing to the view that needs to be fixed. With the second example you (ideally) or users of your site (worst case scenario) will get a runtime error when they visit this particular view.

强类型助手首先在 ASP.NET MVC 2 中引入,我上次使用弱类型助手是很久以前的 ASP.NET MVC 1 应用程序.

Strongly typed helpers were first introduced in ASP.NET MVC 2 and the last time I used a weakly typed helper was in an ASP.NET MVC 1 application long time ago.

这篇关于DropDownlist 或 DropDownListFor Html helper 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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