Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom attribute with dash in name using EditorFor/TextBoxFor/TextBox helpers

I am using Knockout-JS to bind properties in my view to my view model. Knockout-JS uses a custom attribute called 'data-bind' that you have to append to controls in which you want to be bound to view model objects.

Example:

<input type='text' name='first-name' data-bind='value: firstName'/>

Notice the 'data-bind' attribute.

In my view rendering, I am having trouble rendering a textbox that has this attribute. I am aware the Html.EditorFor, Html.TextBoxFor, and Html.TextBox helpers all take an anonymous object that you can use to specify custom attributes. The only problem with this implementation is C# doesn't allow dashes as variable names, so this won't compile: @Html.EditorFor(m => m.FirstName, new { data-bind = "value: firstName" });

The only thing I can think of is this (in view-model):

public class DataBindingInput
{
     public string Value { get; set; }
     public string DataBindingAttributes { get; set }
}

public class MyViewModel
{
    ...
    public DataBindingValue firstName { get; set; }
    ....
}

And a view template called "DataBindingInput.cshtml":

@model DataBindingInput
<input type='text' data-binding='@Model.DataBindingAttributes' value='@Model.Value'>

The only trouble with this is I lose the automatic generation of the input name so it won't work on a post-back because the model binder has no idea how to bind it.

How can I make this work?

like image 409
Michael Brennan Avatar asked Jun 29 '11 19:06

Michael Brennan


People also ask

What is the difference between textbox and TextBoxFor?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.

What is the difference between EditorFor and TextBoxFor?

TextBoxFor will always show the textbox element no matter which kind of property we are binding it with. But EditorFor is much flexible in this case as it decides which element will be more suitable to show the property.

What is EditorFor?

EditorFor<TModel,TValue>(HtmlHelper<TModel>, Expression<Func<TModel,TValue>>, String, String, Object) Returns an HTML input element for each property in the object that is represented by the expression, using the specified template, HTML field name, and additional view data.

What is TextBoxFor?

Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes. TextBoxFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, String, IDictionary<String,Object>) Returns a text input element.


1 Answers

Thanks to Crescent Fish above, looks like you can just use underscores and MVC 3 will convert them to dashes since underscores aren't allowed in HTML attribute names.

like image 134
Michael Brennan Avatar answered Oct 11 '22 12:10

Michael Brennan