Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may take a look at the <a href="http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates-_2D00_-ASP.Net-MVC-2.0-Beta_2D00_1.aspx" rel="noreferrer">following article</a> for writing a custom <code>DataAnnotationsModelMetadataProvider</code>. </p> <p>And here's another, more ASP.NET MVC 3ish way to proceed involving the newly introduced <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.imetadataaware.aspx" rel="noreferrer">IMetadataAware</a> interface.</p> <p>Start by creating a custom attribute implementing this interface:</p> <pre><code>public class PlaceHolderAttribute : Attribute, IMetadataAware { private readonly string _placeholder; public PlaceHolderAttribute(string placeholder) { _placeholder = placeholder; } public void OnMetadataCreated(ModelMetadata metadata) { metadata.AdditionalValues["placeholder"] = _placeholder; } } </code></pre> <p>And then decorate your model with it:</p> <pre><code>public class MyViewModel { [PlaceHolder("Enter title here")] public string Title { get; set; } } </code></pre> <p>Next define a controller:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } } </code></pre> <p>A corresponding view:</p> <pre><code>@model MyViewModel @using (Html.BeginForm()) { @Html.EditorFor(x =&gt; x.Title) &lt;input type="submit" value="OK" /&gt; } </code></pre> <p>And finally the editor template (<code>~/Views/Shared/EditorTemplates/string.cshtml</code>):</p> <pre><code>@{ var placeholder = string.Empty; if (ViewData.ModelMetadata.AdditionalValues.ContainsKey("placeholder")) { placeholder = ViewData.ModelMetadata.AdditionalValues["placeholder"] as string; } } &lt;span&gt; @Html.Label(ViewData.ModelMetadata.PropertyName) @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { placeholder = placeholder }) &lt;/span&gt; </code></pre>
    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. 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