English 中文(简体)
Client Id for Property (ASP.Net MVC)
原标题:

I m trying to do a label for a TextBox in my View and I d like to know, how can I take a Id that will be render in client to generate scripts... for example:

<label for="<%=x.Name.ClientId%>"> Name: </label>
<%=Html.TextBoxFor(x=>x.Name) %>

What do I need to put in "ClientId" to make sure that the correct Id will be rendered to the corresponding control?

最佳回答

Put this code somewhere:

using System; 
using System.Linq.Expressions; 
using System.Web.Mvc; 

namespace MvcLibrary.Extensions 
{ 
    public static class HtmlExtensions 
    { 
        public static MvcHtmlString FieldIdFor<TModel, TValue>(this HtmlHelper<TModel> html,
            Expression<Func<TModel, TValue>> expression) 
        { 
            string htmlFieldName = ExpressionHelper.GetExpressionText(expression); 
            string inputFieldId = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName); 
            return MvcHtmlString.Create(inputFieldId); 
        } 
    } 
}

Then in your ASPX view:

<label for="<%= Html.FieldIdFor(m => m.EmailAddress) %>">E-mail address:</label> 
<%= Html.TextBoxFor(m => m.EmailAddress) %>

You can also use this in JavaScript calls as you won t know the control s ID in advance and may need it for some JavaScript code to work against it:

<script> $.CoolJQueryFunction( <%= Html.FieldIdFor(m => m.EmailAddress) %> ); </script>

The LabelFor HTML helper method, that someone mentioned here, won t let you specify the actual text label you want to use, you have to decorate your ViewModels with attributes to set the label text, with IMHO is ugly. I d rather that stuff appear in the actual ASPX view part itself, not on some domain/view model. Some people will disagree with me though.

Not sure of the rules for posting links to one s blog posts, but I posted a blog on this exact topic: http://www.dominicpettifer.co.uk/Blog/37/strongly-typed--label--elements-in-asp-net-mvc-2

问题回答

MVC 4 has this built in now.. see this

Its fun what can be found at free source code of MVC. Here is the answer:

@Html.IdFor or @Html.NameFor

So what is the difference? Here is the catch:

The NameFor would not replace any "." to "_".

If you want any example, I have found this cool small article

As has been said, you don t need to worry about ClientID, UniqueID etc in ASP.NET MVC as those are webforms abstractions. You can simply just write out the ID that you want. There is also an extension method for this:

<%= Html.LabelFor(x => x.Name) %>

Use the following:

Model.Name

You can simply do a "view source" or examine the rendered textbox with something like Firebug to see what the Html.xyzFor() methods are generating. Normally they would generate a textbox with the "id" and "name" attributes both set to the property name.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签