如何为特定的 MVC 操作使文本框只读?
How do I make a textbox readonly only for a specific MVC Action?
标题可能令人困惑,所以这是我的问题:
仅当我的操作用于编辑(类似于 EditLocation)并且如果我的操作旨在添加新记录(AddLocation)时,我希望在我的视图中有一个只读文本框,我想要一个可编辑的文本框。
我的ff。代码有效,但我想知道是否有 "cleaner" 解决方案
@using (Html.BeginForm(Model.Location.Id == 0 ? "AddLocation" : "EditLocation", "Location"))
{
<fieldset>
@Html.HiddenFor(x => x.Location.Id)
@Html.HiddenFor(x => x.Location.CompanyGroupId)
@Html.HiddenFor(x => x.CompanyGroup.Id)
@Html.HiddenFor(x => x.CompanyGroup.Code)
<div class="form-group">
<strong><span>@ResourcesCommon.Location_Code</span></strong>
@if (Model.Location.Id == 0)
{
@Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Location.Code)
}
else
{
@Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(x => x.Location.Code)
}
</div> ...
谢谢,祝你一周愉快!
这可能对您有帮助:
@Html.TextBoxFor(x => x.Location.Code, new Dictionary<string, object>()
.AddIf(true, "@class", "form-control")
.AddIf(Model.Location.Id != 0, "@readonly", "readonly"))
// This returns the dictionary so that you can "fluently" add values
public static IDictionary<TKey, TValue> AddIf<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, bool addIt, TKey key, TValue value)
{
if (addIt)
dictionary.Add(key, value);
return dictionary;
}
我从另一个 Whosebug post 很久以前拿来的,我没有 link。
您可以这样做(但会产生更多代码):
@Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control", @id="myInput" })
然后添加一些js:
$(document).ready(function() {
$('#myInput').attr('readonly', @Html.Raw(Model.Location.Id != 0? "true" : "false"));
});
标题可能令人困惑,所以这是我的问题: 仅当我的操作用于编辑(类似于 EditLocation)并且如果我的操作旨在添加新记录(AddLocation)时,我希望在我的视图中有一个只读文本框,我想要一个可编辑的文本框。
我的ff。代码有效,但我想知道是否有 "cleaner" 解决方案
@using (Html.BeginForm(Model.Location.Id == 0 ? "AddLocation" : "EditLocation", "Location"))
{
<fieldset>
@Html.HiddenFor(x => x.Location.Id)
@Html.HiddenFor(x => x.Location.CompanyGroupId)
@Html.HiddenFor(x => x.CompanyGroup.Id)
@Html.HiddenFor(x => x.CompanyGroup.Code)
<div class="form-group">
<strong><span>@ResourcesCommon.Location_Code</span></strong>
@if (Model.Location.Id == 0)
{
@Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Location.Code)
}
else
{
@Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(x => x.Location.Code)
}
</div> ...
谢谢,祝你一周愉快!
这可能对您有帮助:
@Html.TextBoxFor(x => x.Location.Code, new Dictionary<string, object>()
.AddIf(true, "@class", "form-control")
.AddIf(Model.Location.Id != 0, "@readonly", "readonly"))
// This returns the dictionary so that you can "fluently" add values
public static IDictionary<TKey, TValue> AddIf<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, bool addIt, TKey key, TValue value)
{
if (addIt)
dictionary.Add(key, value);
return dictionary;
}
我从另一个 Whosebug post 很久以前拿来的,我没有 link。
您可以这样做(但会产生更多代码):
@Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control", @id="myInput" })
然后添加一些js:
$(document).ready(function() {
$('#myInput').attr('readonly', @Html.Raw(Model.Location.Id != 0? "true" : "false"));
});