I have this line of code:
我有这一行代码:
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
I have a variable in my view data dictionary called Readonly. How do I make Quantity read-only if ViewBag.Readonly
is true and not read only if it is false?
我的视图数据字典中有一个变量叫做Readonly。如果ViewBag,我如何使数量为只读。Readonly是true,只有当它是false时才读取?
Simple thing, but the combination of Razor with HTML (which is ancient) makes otherwise simple things impossible.
简单的事情,但是把剃刀和HTML结合起来(这是古老的),使得原本简单的事情变得不可能。
Edits:
编辑:
I don't want to use an if statement. That is a last resort because it violates DRY which I have been severely burned many times in the past for not following.
我不想用if语句。这是最后的办法,因为它违反了干燥,我曾多次被严重烧伤,因为没有遵守。
The line I have above does work insofar as it makes the text box read-only. I need to make this conditional based upon my view state.
上面的行可以使文本框成为只读的。我需要根据我的视图状态设置这个条件。
Solution:
解决方案:
I've used the following. It still is a DRY violation, but it reduces it to one line.
我用以下。这仍然是干的违规,但它把它减少到一行。
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = ViewBag.Readonly ? (object)new { @class = "form-control", @readonly = "htmlsucks" } : (object)new { @class = "form-control" } })
5 个解决方案
#1
18
EDIT: MVC 5
编辑:MVC 5
Controller
控制器
ViewBag.Readonly=true;//false
View
视图
@Html.EditorFor(model => model.Quantity, ViewBag.Readonly ? (object)new { htmlAttributes = new { @readonly = "readonly", @class = "form-control" }} : new { htmlAttributes = new { @class = "form-control" } })
#2
4
It's very simple. Do it like this.
很简单。这样做。
@if((bool)ViewBag.Readonly)
{
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
}
else
{
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
}
#3
2
In case you have many places in your view with such logic, I think using the library FluentDataAnnotations can keep you code clean and clear.
如果您的视图中有许多地方具有这样的逻辑,我认为使用库fluentdataannotation可以保持代码的干净和清晰。
If you are familiar with FluentValidator, it's very similar using.
如果您熟悉FluentValidator,它的用法非常类似。
In your case all the logic will moved from a view to model annotation class. In you will have only @Html.EditorFor(model => model.Quantity)
在您的例子中,所有的逻辑都将从视图移动到模型注释类。您将只有@Html。= > model.Quantity EditorFor(模型)
It even allows to use model properties in conditions e.g. this.When(model => !model.AllowEditPhone, () => { this.For(m => m.Phone).SetReadOnly(false); });
它甚至允许在这样的条件下使用模型属性。当模型= > !模型。AllowEditPhone () => {this。(m = > m.Phone).SetReadOnly(假);});
Here the NuGet package that requires ASP.NET MVC 5. (Support for ASP.NET Core are in progress.)
这里的NuGet包需要ASP。净MVC 5。(支持ASP。网络内核正在开发中。
#4
0
By writing a helper method the DRY principal can be respected.
通过编写辅助方法,DRY principal可以被尊重。
using System.Web.Mvc.Html;
public static MvcHtmlString Concat(this MvcHtmlString first, params MvcHtmlString[] strings)
{
return MvcHtmlString.Create(first.ToString() + string.Concat(strings.Select(s => ( s == null ? "" : s.ToString()))));
}
public static MvcHtmlString ConditionalEditFor<TModel,TValue>(this HtmlHelper<TModel> helper, bool EditCondition, Expression<Func<TModel, TValue>> Expression)
{
helper.ConditionalEditFor(EditCondition,Expression,false);
}
public static MvcHtmlString ConditionalEditFor<TModel, TValue>(this HtmlHelper<TModel> helper, bool EditCondition, Expression<Func<TModel, TValue>> Expression, bool IncludeValidationOnEdit)
{
if (EditCondition)
{
if (!IncludeValidationOnEdit)
return EditorExtensions.EditorFor<TModel, TValue>(helper, Expression);
else
return EditorExtensions.EditorFor<TModel, TValue>(helper, Expression).Concat(ValidationExtensions.ValidationMessageFor<TModel, TValue>(helper, Expression));
}
else
{
return DisplayExtensions.DisplayFor<TModel, TValue>(helper, Expression);
}
}
then in your view:
然后在你的观点:
add a conditional statement to determine readonly e.g.
添加一个条件语句来确定readonly例。
@{bool IsReadOnly = YourCondition;}
@Html.ConditionalEditFor(!IsReadOnly/*condition*/, model => model.YourProperty,true /*do validation*/)
you can then add whatever other overrides you want.
然后,您可以添加任何您想要的其他重写。
#5
0
JQUERY to read SETUP_TYPE control value and disable controls with a particular CSS selector.
JQUERY读取SETUP_TYPE控件值,并使用特定的CSS选择器禁用控件。
$(function () {
if ($("#SETUP_TYPE").val() == "1") { $('.XXX').attr('disabled', true); }
})
$(function () {
if ($("#SETUP_TYPE").val() == "2") { $('.YYY').attr('disabled', true); }
})
This control is disabled if SETUP_TYPE is 1 or 2.
如果SETUP_TYPE是1或2,则禁用此控件。
@Html.EditorFor(model => model.CLAIM, new { htmlAttributes = new { @class = "form-control XXX YYY" } })
This control is disabled if SETUP_TYPE is 1.
如果SETUP_TYPE是1,则禁用此控件。
@Html.EditorFor(model => model.POLICY, new { htmlAttributes = new { @class = "form-control XXX" } })
This control is disabled if SETUP_TYPE is 2.
如果SETUP_TYPE是2,则禁用此控件。
@Html.EditorFor(model => model.INSURED, new { htmlAttributes = new { @class = "form-control YYY" } })
#1
18
EDIT: MVC 5
编辑:MVC 5
Controller
控制器
ViewBag.Readonly=true;//false
View
视图
@Html.EditorFor(model => model.Quantity, ViewBag.Readonly ? (object)new { htmlAttributes = new { @readonly = "readonly", @class = "form-control" }} : new { htmlAttributes = new { @class = "form-control" } })
#2
4
It's very simple. Do it like this.
很简单。这样做。
@if((bool)ViewBag.Readonly)
{
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
}
else
{
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
}
#3
2
In case you have many places in your view with such logic, I think using the library FluentDataAnnotations can keep you code clean and clear.
如果您的视图中有许多地方具有这样的逻辑,我认为使用库fluentdataannotation可以保持代码的干净和清晰。
If you are familiar with FluentValidator, it's very similar using.
如果您熟悉FluentValidator,它的用法非常类似。
In your case all the logic will moved from a view to model annotation class. In you will have only @Html.EditorFor(model => model.Quantity)
在您的例子中,所有的逻辑都将从视图移动到模型注释类。您将只有@Html。= > model.Quantity EditorFor(模型)
It even allows to use model properties in conditions e.g. this.When(model => !model.AllowEditPhone, () => { this.For(m => m.Phone).SetReadOnly(false); });
它甚至允许在这样的条件下使用模型属性。当模型= > !模型。AllowEditPhone () => {this。(m = > m.Phone).SetReadOnly(假);});
Here the NuGet package that requires ASP.NET MVC 5. (Support for ASP.NET Core are in progress.)
这里的NuGet包需要ASP。净MVC 5。(支持ASP。网络内核正在开发中。
#4
0
By writing a helper method the DRY principal can be respected.
通过编写辅助方法,DRY principal可以被尊重。
using System.Web.Mvc.Html;
public static MvcHtmlString Concat(this MvcHtmlString first, params MvcHtmlString[] strings)
{
return MvcHtmlString.Create(first.ToString() + string.Concat(strings.Select(s => ( s == null ? "" : s.ToString()))));
}
public static MvcHtmlString ConditionalEditFor<TModel,TValue>(this HtmlHelper<TModel> helper, bool EditCondition, Expression<Func<TModel, TValue>> Expression)
{
helper.ConditionalEditFor(EditCondition,Expression,false);
}
public static MvcHtmlString ConditionalEditFor<TModel, TValue>(this HtmlHelper<TModel> helper, bool EditCondition, Expression<Func<TModel, TValue>> Expression, bool IncludeValidationOnEdit)
{
if (EditCondition)
{
if (!IncludeValidationOnEdit)
return EditorExtensions.EditorFor<TModel, TValue>(helper, Expression);
else
return EditorExtensions.EditorFor<TModel, TValue>(helper, Expression).Concat(ValidationExtensions.ValidationMessageFor<TModel, TValue>(helper, Expression));
}
else
{
return DisplayExtensions.DisplayFor<TModel, TValue>(helper, Expression);
}
}
then in your view:
然后在你的观点:
add a conditional statement to determine readonly e.g.
添加一个条件语句来确定readonly例。
@{bool IsReadOnly = YourCondition;}
@Html.ConditionalEditFor(!IsReadOnly/*condition*/, model => model.YourProperty,true /*do validation*/)
you can then add whatever other overrides you want.
然后,您可以添加任何您想要的其他重写。
#5
0
JQUERY to read SETUP_TYPE control value and disable controls with a particular CSS selector.
JQUERY读取SETUP_TYPE控件值,并使用特定的CSS选择器禁用控件。
$(function () {
if ($("#SETUP_TYPE").val() == "1") { $('.XXX').attr('disabled', true); }
})
$(function () {
if ($("#SETUP_TYPE").val() == "2") { $('.YYY').attr('disabled', true); }
})
This control is disabled if SETUP_TYPE is 1 or 2.
如果SETUP_TYPE是1或2,则禁用此控件。
@Html.EditorFor(model => model.CLAIM, new { htmlAttributes = new { @class = "form-control XXX YYY" } })
This control is disabled if SETUP_TYPE is 1.
如果SETUP_TYPE是1,则禁用此控件。
@Html.EditorFor(model => model.POLICY, new { htmlAttributes = new { @class = "form-control XXX" } })
This control is disabled if SETUP_TYPE is 2.
如果SETUP_TYPE是2,则禁用此控件。
@Html.EditorFor(model => model.INSURED, new { htmlAttributes = new { @class = "form-control YYY" } })