ASP.NET MVC 3:覆盖“名称"带有 TextBoxFor 的属性 [英] ASP.NET MVC 3: Override "name" attribute with TextBoxFor

查看:23
本文介绍了ASP.NET MVC 3:覆盖“名称"带有 TextBoxFor 的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Html.TextBoxFor来覆盖name属性?

Is it possible when using Html.TextBoxFor to override the name attribute?

我试过没有成功.我需要使用 TextBoxFor 来让客户端验证工作,但是由于我不会进入的原因,我需要文本框的名称与生成的名称不同.

I have tried with no success. I need to use TextBoxFor to get client side validation to work, however for reasons I won't go into I need the name of the textbox to be different from the generated one.

我尝试了以下方法:

@Html.TextBoxFor(x => x.Data, new { name = Model.Key + "_Data", id = Model.Key + "_Data" })

适用于 ID 但不适用于名称.这可能吗?

Which works for ID but not name. Is this possible?

更新:查看 TextBoxFor 的代码.看起来没有简单的方法.希望有人能证明我是错的.

Update: Looking into the code for TextBoxFor. It doesn't look like there is an easy way. Hopefully someone can prove me wrong.

推荐答案

Rob,其实还有更简单的方法.使用 Name 代替 name:

Rob, actually there is a much simpler way. Instead of name, use Name:

@Html.TextBoxFor(x => x.Data, new { Name = Model.Key + "_Data", id = Model.Key + "_Data" })

这篇关于ASP.NET MVC 3:覆盖“名称"带有 TextBoxFor 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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