views:

91

answers:

4

Hi guys...

I'm begginer in asp.net mvc, and i have a doubs:

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 generete scripts... for example:

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

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

Thanks

Cheers

A: 

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.

womp
it'll write the content of the property, and I don't need it.. i need the Name of the Property (or Client Id... if there is one... i don't know excatly)
Felipe
A: 

lol :D, ClientID, the guy is looking for a clientID in asp.net mvc


there is no web controls in here so you set the ids by yourself Html.Textbox, Html.LabelFor are helpers that generate html but you can write the stuff withouth them it still going to work
in your case Html.TextBoxFor(x => x.Name) will do <input id="Name"
that's your clientID

Omu
Omu, it's still a valid question. If you use the HTML helpers that end with For (LabelFor, TextBoxFor, DropDownFor etc etc.), they generate the id and name attributes automatically based on the ViewModel's Property name. It's suppose to give you strong typing and compile time safety over just typing out a string. I guess Felipe wants to maintain that compile time safety but for Labels as well. Nothing wrong with that.
Sunday Ironfoot
A: 

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) %>
richeym
LabelFor will give you a strongly typed <label> but doesn't let you specify the actual label text itself, you have to set it against the ViewModel itself as a Property Attribute, this is a weird omission to me.
Sunday Ironfoot
+1  A: 

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

Sunday Ironfoot
I know, but some people can't understand correctly (my english isn't very well)! That's all I need... Thanks a lot Man! Decorate domain model classes with attributies isn't a good pratice for me, I agree with you!
Felipe