@ Html.DropDownList ()를 사용하여 선택할 CSS 클래스 추가
수년간 웹 양식을 수행 한 후 첫 번째 MVC 응용 프로그램을 만들고 있는데 어떤 이유로이 작업을 수행 할 수 없습니다.
@Html.DropDownList("PriorityID", String.Empty, new {@class="textbox"} )
에러 메시지:
System.Web.Mvc.HtmlHelper<SPDR.Models.Bug>
'에 대한 정의가 포함되어 있지 않으며 DropDownList
최상의 확장 메서드 오버로드 System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>, object)
에 잘못된 인수가 있습니다.
어떤 도움이라도 대단히 감사합니다!
컨트롤러를보고 MVC가 실제로 작동하는 방식에 대해 조금 더 자세히 살펴보면이 점을 이해할 수있었습니다.
내 뷰는 자동 생성 된 뷰 중 하나였으며 다음 코드 줄을 포함했습니다.
@Html.DropDownList("PriorityID", string.Empty)
html 속성을 추가하려면 다음과 같이해야합니다.
@Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { @class="dropdown" })
도움을 주신 @Laurent에게 다시 한 번 감사드립니다. 질문이 명확하지 않았 음을 깨달았습니다.
최신 정보:
이 작업을 수행하는 더 좋은 방법은 가능한 경우 DropDownListFor 를 사용 하는 것입니다. 그러면 이름 속성에 대한 매직 문자열에 의존하지 않습니다.
@Html.DropDownListFor(x => x.PriorityID, (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { @class = "dropdown" })
오류 메시지의 서명에서 알 수 있듯이 두 번째 인수는 IEnumerable,보다 구체적으로는 SelectListItem의 IEnumerable이어야합니다. 선택 목록입니다. SelectListItem의 IEnumerable 인 SelectList 유형을 사용할 수 있습니다. 선택 사항이없는 목록의 경우 :
@Html.DropDownList("PriorityID", new List<SelectListItem>(), new {@class="textbox"} )
몇 가지 선택 사항이있는 목록 :
@Html.DropDownList(
"PriorityID",
new List<SelectListItem>
{
new SelectListItem { Text = "High", Value = 1 },
new SelectListItem { Text = "Low", Value = 0 },
},
new {@class="textbox"})
이 튜토리얼이 도움이 될 수 있습니다 : ASP.NET MVC로 DropDownList를 만드는 방법
Asp.Net MVC에서 인수 이상의 드롭 다운 목록을 추가하는 경우. 레코드를 편집하거나 뷰백에서 값을 전달할 때.
이것을 사용하면 작동합니다.
@Html.DropDownList("CurrencyID",null,String.Empty, new { @class = "form-control-mandatory" })
jQuery를 사용하여 할 수 있습니다.
$("select").addClass("form-control")
여기에서 is- html 태그, Form-control is- 클래스 이름 선택
@Html.DropDownList("SupplierId", "Select Supplier")
여기서 SupplierId는 ViewBagList, Select Supplier는-표시 이름입니다.
dropdownList가없고 다음과 같이 사용할 수있는 CSS 클래스를 삽입하려는 경우 생성자보기에 몇 가지 옵션이 있습니다.
@Html.DropDownList("Country", null, "Choose-Category", new {@class="form-control"})
이 경우 Country는 드롭 다운의 이름이고 null은 컨트롤러에서 일반 목록을 전달하지 않기위한 것입니다. "Choose-Category"가 선택된 항목이고 기본 옵션을 선택하지 않으려면 CSS 클래스의 마지막 항목입니다. "Choose-Category"를 ""로 간단히 교체
간단히 시도하십시오
@Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { @class="dropdown" })
그러나 기본값을 원하거나 옵션 값이 없으면 이것을 시도해야합니다. 기본 옵션으로 String.Empty
작동 할 값이 선택되지 않기 때문 입니다.-select-
@Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, String.Empty, new { @class="dropdown" })
이 시도:
@Html.DropDownList(
"country",
new[] {
new SelectListItem() { Value = "IN", Text = "India" },
new SelectListItem() { Value = "US", Text = "United States" }
},
"Country",
new { @class = "form-control",@selected = Model.Country}
)
아래 코드를 시도하십시오.
@Html.DropDownList("ProductTypeID",null,"",new { @class = "form-control"})
간단하게 다음과 같이 할 수 있습니다.
@Html.DropDownList("PriorityID", null, new { @class="form-control"})
이 시도
@Html.DropDownList("Id", null, new { @class = "ct-js-select ct-select-lg" })
참고URL : https://stackoverflow.com/questions/7367344/adding-a-css-class-to-select-using-html-dropdownlist
'Development Tip' 카테고리의 다른 글
Perl : if (목록의 요소) (0) | 2020.11.03 |
---|---|
파이썬에서 정수를 반올림하는 방법 (0) | 2020.11.03 |
git에서 커밋 한 후 자동으로 푸시하는 방법은 무엇입니까? (0) | 2020.11.03 |
Angularjs 필터가 null이 아닙니다. (0) | 2020.11.03 |
npm 구성을 기본값으로 복원 / 재설정하는 방법은 무엇입니까? (0) | 2020.11.03 |