mvc에서 사용자 지정 텍스트 상자를 만들려고합니다. 기본 클래스 값 "text-uppercase"를 추가하고 싶습니다. 또한보기에 새 클래스 값을 추가하고 싶습니다.
Myhelper 클래스는 다음과 같습니다.
public static MvcHtmlString Custom_TextBox<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata oModelMetadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
htmlAttributes.Add("type", "text");
htmlAttributes.Add("name", oModelMetadata.DisplayName);
htmlAttributes.Add("id", oModelMetadata.DisplayName);
htmlAttributes.Add("class", "text-uppercase");
return helper.TextBoxFor(expression, htmlAttributes);
}
그리고 내보기 코드 :
@Html.Custom_TextBox(model => model.YazilimAdi,new { @class = "form-control" } )
실제로 오류는 다음과 같습니다. 동일한 키를 가진 항목이 이미 추가되었습니다. "수업".
기본 클래스 값 "text-uppercase"를 어떻게 추가 할 수 있습니까?
if(htmlAttributes.ContainsKey("class"))
{
htmlAttributes["class"] += " text-uppercase";
}
else
{
htmlAttributes.Add("class", "text-uppercase");
}
그리고 이것을 확장의 시작 부분에 넣으십시오.
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다