views:

185

answers:

2

I'm curently refactoring my MVC2 code base to use the ViewModel approach versus. As a result, the models I'm =passing to my views to render forms now look like

model.TheObject
model.TheCollectionOfOtherObjects

I'm trying to use the HTML.EditorFor helper to render a Template Editor for a data type. It used to work when I was trying to render TheObject directly in the view, now that there's a level of indirection, I'm getting a totally different default object naming scheme:

For example:

Before I did the refactoring into the ViewModel, I output a special JQuery based date picker:

<%: Html.EditorFor(model => model.StartDate, String.Format("{0:M/d/yyyy}", Model.StartDate))%> 

Output:

<input id="StartDate" name="StartDate" type="text" value="7/5/2010 11:10:25 AM" />            

All was happy in JQuery land.

Then I changed the view to use a ViewModel that contained TheObject and changed my EditorFor code to the following:

    <%: Html.EditorFor(model => model.TheObject.StartDate, String.Format("{0:M/d/yyyy}", Model.TheObject.StartDate))%> 

Now renders the following HTML:

    <input id="TheObject_StartDate" name="TheObject.StartDate" type="text" value="7/5/2010 11:10:25 AM" />    

This, naturally breaks my JQuery on the client side, and I'd prefer to have more control over output the id and name attributes on the html element.

Any ideas on how I change that lamba expression that I pass into EditorFor

Thanks

+1  A: 

Hey,

Why not just use CSS selectors instead, or match the ID but look at the end like $("input[id$='StartDate']")?

There is only so much you can control with this approach; could just use Html.TextBox and not use the For syntax. That gives you complete control...

The TextBoxFor approach, with the ID it gives, ensures that any posts to the server can recreate the model trail of the same object type...

HTH.

Brian
I didn't even think about changing my Editor template to use the regular Html.TextInput. However, that didn't do what I was expecting in the naming convention, so I went and wrote some regular HTML input elements and based the name on ViewData.ModelMetadata.PropertyName.
taudep
I unchecked it as answered as I don't like my workaround because it screws up ModelBinding.
taudep
If the same object being loaded is the object being posted, you have to create a workaround for your script to not mess up the model binding. If the object being form posted is different, then you can render html.TextBox, define the name as the name of the property of the object, and it should work fine. If not, post a sample. It works for me.
Brian
I can't figure this out. Deep in my template of a nested object, I can't get the id that MVC is creating when rending the text area. I'm trying ViewData.TemplateInfo.HtmlFieldPrefix, but that returns "ThisObject.PropertyName" instead of "ThisObject_PropertyName". The gist of what I'm doing is dynamically populating a JQuery Selector based on the html input value that's created and bound to in this EditorFor template. Seems like it should be easy simply...
taudep
My sad workaround for now is in the Template to generate the name of the JQuery selector with the following snippet: <%= ViewData.TemplateInfo.HtmlFieldPrefix.Replace('.', '_') %> I'd love for something better, though.
taudep
OK, not 100% sure if there is a better solution from the server, though question: could you append a CSS style to the editor for start date, and target it using $("#parentwrapper").find(".StartDateControl") instead?
Brian
A: 

Bit late, but for the benefit of others there is the Html.IdFor() that would allow you to get the generated ID. You could then write that into the JavaScript for JQuery to use.

<%: Html.IdFor(model => model.TheObject.StartDate) %>
Meff
IT shoulud be noted that this is a ASP.NET v3.0 feature. It doesn't exist in ASP.NET MVC before that.
taudep