Sunday 15 January 2012

asp.net mvc - Ignore "Display" attributes in Razor View -



asp.net mvc - Ignore "Display" attributes in Razor View -

i'm trying acheive dry principle using oop view models:

public class itembase { [display(name = "description - displayed in list (so create meaningful)")] [required] public string description { get; set; } [display(name = "username")] [required] public string username { get; set; } [display(name = "optional secondary credential")] public string secondcredential { get; set; } [display(name = "location - ideally url")] [required] public string location { get; set; } [display(name = "additional notes")] public string notes { get; set; } } public class itemdisplay : itembase { [required] public int32 itemid { get; set; } public applicationuser creator { get; set; } } public class itemedit : itembase { [required] public int32 itemid { get; set; } } public class itemadd : itemase { [required] public int32 parent_categoryid { get; set; } }

this work great... however, when want utilize itemdisplay class display info in view, displays display attribute data. how view ignore these without putting of members itemdisplay class without display attrbute?

view code

<article class="first"> <h2>details</h2> <div class="form-horizontal toppaddding"> <div class="form-group"> @html.labelfor(model => model.viewitem.description, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.raw(model.viewitem.description) </div> </div> <div class="form-group"> @html.labelfor(model => model.viewitem.username, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.raw(model.viewitem.username) </div> </div> <div class="form-group"> @html.labelfor(model => model.viewitem.secondcredential, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.raw(model.viewitem.secondcredential) </div> </div> <div class="form-group"> @html.labelfor(model => model.viewitem.password, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.raw(model.viewitem.password) </div> </div> <div class="form-group"> @html.labelfor(model => model.viewitem.location, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.raw(model.viewitem.location) </div> </div> <div class="form-group"> @html.labelfor(model => model.viewitem.notes, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.raw(model.viewitem.notes) </div> </div> </div> </article>

the html.labelfor helper first looks display dataannotation , if there not one, default outputting name of property in class.

if not want utilize display info annotation in particular view, don't utilize html.labelfor helper , instead either create own label tag or output whatever before <div> containing model data.

<div class="form-group"> <label class = "control-label col-md-2">alternate description label text</label> <div class="col-md-10"> @html.raw(model.viewitem.description) </div> </div>

in case, not creating input form, may think using span instead of label.

asp.net-mvc razor

No comments:

Post a Comment