如何防止 EditorTemplate 逻辑覆盖 View 逻辑?
How can I prevent EditorTemplate logic from overriding View logic?
我使用 MVC5 添加了视图逻辑以根据模型中的值禁用数据输入。逻辑运行后,它会被现有的 EditorTemplate 逻辑覆盖 - 该字段保持启用状态。我怎样才能让我的禁用逻辑工作?此视图逻辑首先运行:
<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>
那么冲突的EditorTemplate代码:
@model DateTime
@Html.TextBox(string.Empty, @Model.ToString("MM/dd/yyyy"), new { @class = "form-control datepicker" })
当您调用 @Html.EditorFor(model => model.Property, attributesObj)
并定义了一个自定义编辑器视图时,您可以看到该函数的智能感知表明 htmlAttributes 对象也包含在视图的 ViewData
中。换句话说,如果您在编辑器视图中访问 ViewData
属性,您应该有权访问从 @Html.EditorFor(model => model.OnHold.DateIssued, attributes);
语句传递的属性。这是一个简单的例子:
// Index.cshtml - assuming model.Message is of type string
@Html.EditorFor(model => model.Message, new { @class = "text-danger" })
// EditorTemplates/String.cshtml
@model string
@{
object textboxAttributes = new { @class = "text-success" };
// Override the default @class if ViewData property contains that key
if (ViewData.ContainsKey("class") && string.IsNotNullOrWhitespace(ViewData["class"].ToString())
{
textboxAttributes = new { @class = ViewData["class"] };
}
}
@Html.TextboxFor(model => model, textboxAttributes)
在您的情况下,您的 EditorTemplate 现在将如下所示:
@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)
我使用 MVC5 添加了视图逻辑以根据模型中的值禁用数据输入。逻辑运行后,它会被现有的 EditorTemplate 逻辑覆盖 - 该字段保持启用状态。我怎样才能让我的禁用逻辑工作?此视图逻辑首先运行:
<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>
那么冲突的EditorTemplate代码:
@model DateTime
@Html.TextBox(string.Empty, @Model.ToString("MM/dd/yyyy"), new { @class = "form-control datepicker" })
当您调用 @Html.EditorFor(model => model.Property, attributesObj)
并定义了一个自定义编辑器视图时,您可以看到该函数的智能感知表明 htmlAttributes 对象也包含在视图的 ViewData
中。换句话说,如果您在编辑器视图中访问 ViewData
属性,您应该有权访问从 @Html.EditorFor(model => model.OnHold.DateIssued, attributes);
语句传递的属性。这是一个简单的例子:
// Index.cshtml - assuming model.Message is of type string
@Html.EditorFor(model => model.Message, new { @class = "text-danger" })
// EditorTemplates/String.cshtml
@model string
@{
object textboxAttributes = new { @class = "text-success" };
// Override the default @class if ViewData property contains that key
if (ViewData.ContainsKey("class") && string.IsNotNullOrWhitespace(ViewData["class"].ToString())
{
textboxAttributes = new { @class = ViewData["class"] };
}
}
@Html.TextboxFor(model => model, textboxAttributes)
在您的情况下,您的 EditorTemplate 现在将如下所示:
@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)