Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@pszmyd: You can use the ViewDataDictionary that already takes an object in the constructor ex. </p> <pre><code>// // Summary: // Initializes a new instance of the System.Web.Mvc.ViewDataDictionary class // by using the specified model. // // Parameters: // model: // The model. public ViewDataDictionary(object model); </code></pre> <p>Also what you are trying to do is simple enough - merging key values. In the case of html attributes it is not straight forward. ex. element can contain multiple classes ex. 'blue dr ltr', separated by spaces, while the style attribute uses the semi-colon as delimiter ex. 'width:200px; font-size:0.8em;'. It is really up to you to parse and check that the values are correct (not merging css classes instead of splitting them with spaces, same with the style).</p> <p>I'd suggest from your parameter:<code>object htmlAttributes</code> you just create: var <code>htmlAttr = new ViewDataDictionary&lt;TModel&gt;(htmlAttributes);</code></p> <p>then create custom extension methods to merge the attributes ex.</p> <pre><code>public static class ViewDataDictionaryExtensions { public static ViewDataDictionary&lt;TModel&gt; MergeClasses&lt;TModel&gt;(this ViewDataDictionary&lt;TModel&gt; dict, string classes) { if (dict.ContainsKey("class")) dict["class"] += " " + classes; else dict.Add("class", classes); return dict; } public static ViewDataDictionary&lt;TModel&gt; MergeStyles&lt;TModel&gt;(this ViewDataDictionary&lt;TModel&gt; dict, string styles) { if (dict.ContainsKey("style")) dict["style"] += "; " + styles; else dict.Add("style", styles); return dict; } } </code></pre> <p>This is only a really simple implementation not taking into account duplicate attribute values or multiple separators. But hope you get the idea!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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