Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is, your template can contain several HTML elements, so MVC won't know to which one to apply your size/class. You'll have to define it yourself.</p> <p>Make your template derive from your own class called TextBoxViewModel:</p> <pre><code>public class TextBoxViewModel { public string Value { get; set; } IDictionary&lt;string, object&gt; moreAttributes; public TextBoxViewModel(string value, IDictionary&lt;string, object&gt; moreAttributes) { // set class properties here } public string GetAttributesString() { return string.Join(" ", moreAttributes.Select(x =&gt; x.Key + "='" + x.Value + "'").ToArray()); // don't forget to encode } </code></pre> <p>}</p> <p>In the template you can do this:</p> <pre><code>&lt;input value="&lt;%= Model.Value %&gt;" &lt;%= Model.GetAttributesString() %&gt; /&gt; </code></pre> <p>In your view you do:</p> <pre><code>&lt;%= Html.EditorFor(x =&gt; x.StringValue) %&gt; or &lt;%= Html.EditorFor(x =&gt; new TextBoxViewModel(x.StringValue, new IDictionary&lt;string, object&gt; { {'class', 'myclass'}, {'size', 15}}) %&gt; </code></pre> <p>The first form will render default template for string. The second form will render the custom template.</p> <p>Alternative syntax use fluent interface:</p> <pre><code>public class TextBoxViewModel { public string Value { get; set; } IDictionary&lt;string, object&gt; moreAttributes; public TextBoxViewModel(string value, IDictionary&lt;string, object&gt; moreAttributes) { // set class properties here moreAttributes = new Dictionary&lt;string, object&gt;(); } public TextBoxViewModel Attr(string name, object value) { moreAttributes[name] = value; return this; } </code></pre> <p>}</p> <pre><code> // and in the view &lt;%= Html.EditorFor(x =&gt; new TextBoxViewModel(x.StringValue).Attr("class", "myclass").Attr("size", 15) %&gt; </code></pre> <p>Notice that instead of doing this in the view, you may also do this in controller, or much better in the ViewModel:</p> <pre><code>public ActionResult Action() { // now you can Html.EditorFor(x =&gt; x.StringValue) and it will pick attributes return View(new { StringValue = new TextBoxViewModel(x.StringValue).Attr("class", "myclass").Attr("size", 15) }); } </code></pre> <p>Also notice that you can make base TemplateViewModel class - a common ground for all your view templates - which will contain basic support for attributes/etc.</p> <p>But in general I think MVC v2 needs a better solution. It's still Beta - go ask for it ;-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload