asp.net mvc - How can I make a reusable razor view that requires generic expressions? -
the mvc templates generate views display each field this:
<div class="form-group"> @html.labelfor(function(model) model.name, htmlattributes:=new {.class = "control-label col-md-2"}) <div class="col-md-10"> @html.editorfor(function(model) model.name, new { .htmlattributes = new { .class = "form-control" } }) @html.validationmessagefor(function(model) model.name, "", new { .class = "text-danger" }) </div> </div> there's 1 of these each property--only property name changes--and set of these each type of entity. i'd set reusable partial view, problem look depends on model type, , can't have generic view. have dozens of these in app , keeping them in sync styles alter pain.
so how modularize view pass in look this:
@html.partial("edit property", function(model) model.name) or there more general solution problem i'm not seeing?
use editor templates. i've got write on blog here, tl;dr version each type of thing (could type, class or value; 1 of members of datatype enum; or virtually want via uihint), create partial view in views\shared\editortemplates. within views can set whatever html you'd rendered when calling html.editorfor property.
for example, if had property:
public datetime foo { get; set; } and added next partial view @ views\shared\editortemplates\datetime.cshtml:
<div class="form-group"> @html.label("", htmlattributes:=new {.class = "control-label col-md-2"}) <div class="col-md-10"> @html.textbox("", new { .htmlattributes = new { .class = "form-control" } }) @html.validationmessage("", "", new { .class = "text-danger" }) </div> </div> then, you'd have in view is:
@html.editorfor(m => m.foo) and html rendered.
asp.net-mvc razor
No comments:
Post a Comment