c# - How can I prevent EditorTemplate logic from overriding View logic? -
using mvc5 i've added view logic disable data entry based on value in model. after logic runs overridden existing editortemplate logic - field remains enabled. how can make disable logic work? view logic runs first:
<div> @{ object attributes = new { @class = "form-control", @disabled = "disabled", @readonly = "readonly" }; if (model.onhold.number == 0) { attributes = new { @class = "form-control datepicker" }; } @html.editorfor(model => model.onhold.dateissued, attributes); } </div>
then conflicting editortemplate code:
@model datetime @html.textbox(string.empty, @model.tostring("mm/dd/yyyy"), new { @class = "form-control datepicker" })
when call @html.editorfor(model => model.property, attributesobj)
, have custom editor view defined, can see intellisense function says htmlattributes object included in viewdata
of view. in other words, if access viewdata
property within editor view, should have access properties you're passing @html.editorfor(model => model.onhold.dateissued, attributes);
statement. here's simple example:
// index.cshtml - assuming model.message of type string @html.editorfor(model => model.message, new { @class = "text-danger" }) // editortemplates/string.cshtml @model string @{ object textboxattributes = new { @class = "text-success" }; // override default @class if viewdata property contains key if (viewdata.containskey("class") && string.isnotnullorwhitespace(viewdata["class"].tostring()) { textboxattributes = new { @class = viewdata["class"] }; } } @html.textboxfor(model => model, textboxattributes)
in case editortemplate this:
@model datetime @{ object dateattributes = new { @class = "form-control datepicker" }; if (viewdata.containskey("disabled") && !string.isnullorwhitespace(viewdata["class"].tostring())) { dateattributes = new { @class = viewdata["class"], @readonly = "readonly", @disabled = "disabled" }; } } @html.textbox(string.empty, @model.tostring("mm/dd/yyyy"), dateattributes)
Comments
Post a Comment