모델 속성을 기반으로 내 mvc 응용 프로그램에서 드롭 다운 목록을 비활성화하거나 활성화하려고합니다.
내가하는 일은 :-
@Html.DropDownListFor(m => m.ParentOrganisationID, new SelectList(Model.ParentOrganisations, "ID", "Name", Model.ParentOrganisationID), new { @id = "ddlParentOrganisations", @class = "form-control css-select", @disabled = Model.IsReadOnly ? "disabled" : "false", @style = "width:40%; height:10%;" })
그러나 모델 속성 "model.IsReadOnly"가 false 인 경우에도 드롭 다운이 비활성화 된 것으로 표시됩니다.
자바 스크립트를 사용하지 않고이를 처리하는 방법을 제안하십시오.
미리 감사드립니다
DropDownListFor
html 속성에 대한 개체를 예상하는 c # 코드 줄 (if 조건 포함)을 전달할 수 없기 때문에 도우미 메서드 호출 내에 조건 (if / ternary 문)을 포함 할 수 없습니다. 또한 아래의 모든 마크 업은 비활성화 된 SELECT를 렌더링합니다.
<select disabled></select>
<select disabled="disabled"></select>
<select disabled="false"></select>
<select disabled="no"></select>
<select disabled="usingJqueryEnablePlugin"></select>
<select disabled="enabled"></select>
if 조건으로 Model 속성의 값을 확인하고 비활성화 된 버전을 조건부로 렌더링 할 수 있습니다.
@if (!Model.IsReadOnly)
{
@Html.DropDownListFor(s => s.ParentOrganisationID,
new SelectList(Model.ParentOrganisations, "ID", "Name"))
}
else
{
@Html.DropDownListFor(s => s.ParentOrganisationID,
new SelectList(Model.ParentOrganisations, "ID", "Name"),new {disabled="disabled"})
}
if 조건 검사를 처리하는 사용자 지정 html 도우미 메서드를 만드는 것을 고려할 수 있습니다.
public static class DropDownHelper
{
public static MvcHtmlString MyDropDownListFor<TModel, TProperty>
(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectItems,
object htmlAttributes,
bool isDisabled = false)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression,
htmlHelper.ViewData);
IEnumerable<SelectListItem> items =
selectItems.Select(value => new SelectListItem
{
Text = value.Text,
Value = value.Value,
Selected = value.Equals(metadata.Model)
});
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (isDisabled && !attributes.ContainsKey("disabled"))
{
attributes.Add("disabled", "disabled");
}
return htmlHelper.DropDownListFor(expression,items, attributes);
}
}
이제 면도기보기에서이 도우미를 호출합니다.
@Html.MyDropDownListFor(s=>s.ParentOrganisationID,
new SelectList(Model.ParentOrganisations, "ID", "Name")
,new { @class="myClass"},Model.IsReadOnly)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다