1. Static
@Html.DropDownListFor(model => model.Gender,
new SelectList(new[] { "Male", "Female" }),
"--Select--")
2. Using View Data
View
@Html.DropDownListFor(model => model.Country,
new SelectList(((System.Collections.IEnumerable)ViewData["CList"]),
"Value", "Text"), "—Select”)
Controller
List<DTO> lstadto = Getlist();
ViewData["CList"] = lstadto;
3. Using Model List
View
@(Html.DropDownListFor(model =>
model.Country, new SelectList(Model.CountryList,
"Value", "Text"), new { @class =
"txtbox", id = "ddlcid" }))
Controller
Model dto=new
model();
Dto.CountryList=getlist();
Return View(dto);
4. Using script jsonresult
Script
var URL =
'@Url.Action("GetCountry")';
$.getJSON(URL, function (data) {
$('.ddlcountry').html(data);
});
View
@(Html.DropDownListFor(model => model.Country,
new SelectList(Model.CountryList, "CID",
"CName"), new { @class = "txtbox", id =
"ddlcountry" }))
Controller
public JsonResult GetCountry()
{
string code = string.Empty;
StringBuilder sb = new
StringBuilder();
List<DTO> lstadto
= FillCountries();
foreach (var data in lstadto)
{
sb.Append("<option
value='" + data.CID + "' >" + data.CName + " </option> ");
}
return Json(sb.ToString(),
JsonRequestBehavior.AllowGet);
}
5. Using script add options
Script
$('.ddlcountry').html(null);
$('.ddlcountry').append($('<option/>', {
value: 0,
text: "-- Select --"
}));
View
@(Html.DropDownListFor(model =>
model.Country, new SelectList(Model.Countrylst,
"CID", "CName"), new { @class =
"txtbox", id = "ddlcountry" }))
6. Using script
Script
var url =
'@Url.Content("../Clinic/Getdoc")';
$.getJSON(url, { dep: 1 }, function (data) {
$(ddltarget).empty();
$("#doc").append("<option value=''>Select </option>");
$.each(data, function (index,
Data) {
$("#doc").append("<option value='" + Data.UID +
"'>" + Data.FName + "</option>");
});
View
@Html.DropDownList("doc",
Enumerable.Empty<SelectListItem>(),
new { @style = "width: 175px;" })
Controller
public JsonResult
Getdoc(int dep)
{
JsonResult result = new
JsonResult();
var dt = _icserv.DocList(dep);
result.Data = dt;
result.JsonRequestBehavior =
JsonRequestBehavior.AllowGet;
return result;
}
No comments:
Post a Comment