ASP.NET MVC 2点替换为下划线在元素名称 [英] ASP.NET MVC 2 Dots Replaced With Underscore In Element Name

查看:202
本文介绍了ASP.NET MVC 2点替换为下划线在元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC 2点替换为下划线在元素名称(即使ASP.NET MVC将投入点在默认情况下,name属性!)

当您在ASP.NET MVC窗体上坚持一个元素,它通常执行以下操作:

 <%= Html.TextBoxFor(型号=> model.Contact.FirstName)%GT;

变为

 <输入类型=文本名称=Contact.FirstNameID =Contact_FirstName...

这是一切都很好。但是,如果你再要做到这一点:

 <%= Html.DropDownList(
    CONTACT_TITLE
    新的SelectList(Model.Titles,键,值))%GT;

您实际上结束了

 <选择ID =CONTACT_TITLENAME =CONTACT_TITLE> ...

请注意,你现在有一个下划线不能在名称属性点。

所以,我想我会在传递的名字,其中包括一个点,就像这样:

 <%= Html.DropDownList(
    CONTACT_TITLE
    新的SelectList(Model.Titles,键,值),
    新{NAME =联系\\\\标题。})%GT;

但它仍然呈现为:

 <选择ID =CONTACT_TITLENAME =CONTACT_TITLE> ...

我真的(真的)希望这呈现为:

 <选择ID =CONTACT_TITLENAME =Contact.Title> ...

在为了这个自动绑定回Model.Contact.Title - ?任何想法

重要更新

这是我最初的想法略有不同......它实际上看起来像什么我传递的name属性会被忽略......

 新{NAME =MYNAME}

仍然导致CONTACT_TITLE!

任何帮助AP preciated!


解决方案

 <%= Html.DropDownList(
    Contact.Title
    新的SelectList(Model.Titles,键,值))%GT;

这会使:

 <选择ID =CONTACT_TITLENAME =Contact.Title> ...

它只是因为它不是在XHTML有效的替换在id点。

ASP.NET MVC 2 Dots Replaced With Underscore In Element Name (even though ASP.NET MVC will put dots in by default in the name attribute!)

When you stick an element on a form in ASP.NET MVC, it usually does the following:

<%= Html.TextBoxFor(model => model.Contact.FirstName)%>

Becomes

<input type="text" name="Contact.FirstName" id="Contact_FirstName" ...

This is all well and good. However, if you then want to do this:

<%= Html.DropDownList(
    "Contact_Title",
    new SelectList(Model.Titles, "Key", "Value"))%>

You actually end up with

<select id="Contact_Title" name="Contact_Title">...

Note that you now have an underscore not a dot in the name attribute.

So I thought I'd pass in the name, including a dot, like this:

<%= Html.DropDownList(
    "Contact_Title", 
    new SelectList(Model.Titles, "Key", "Value"),
    new { name = "Contact\\.Title" })%>

But it still renders as:

<select id="Contact_Title" name="Contact_Title">...

I really (really) want this to render as:

<select id="Contact_Title" name="Contact.Title">...

In order for this to bind back to Model.Contact.Title automatically - any ideas?

IMPORTANT UPDATE

This is slightly different to my initial thoughts... it actually looks like whatever I pass as the name attribute is simply ignored...

new { name = "MYNAME" }

Still results in Contact_Title!

Any help appreciated!

解决方案

<%= Html.DropDownList(
    "Contact.Title",
    new SelectList(Model.Titles, "Key", "Value"))%>

This will render:

<select id="Contact_Title" name="Contact.Title">...

It only replaces the dot in the id as it's not valid in xhtml.

这篇关于ASP.NET MVC 2点替换为下划线在元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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