How To Create Asp.net Mvc Dropdownlist With Required Validation
I working with mvc 5. I am loading data from Database Using ORM and fill a drop down list from the controller, like this. ViewBag.Country_id = new SelectList(_db.Countries, 'Countr
Solution 1:
There are few ways to work with DropDownList. I personally like to use Strongly-Type ViewModel instead of ViewBag.
Screen Shot
Validation message displays when submit button is clicked without selecting Country.
Entity
publicclassCountry
{
publicint Country_id { get; set; }
publicstring Description { get; set; }
}
Model
publicclassCountryViewModel
{
[Display(Name = "Country")]
[Required(ErrorMessage = "{0} is required.")]
publicint SelectedCountryId { get; set; }
public IList<SelectListItem> AvailableCountries { get; set; }
publicCountryViewModel()
{
AvailableCountries = new List<SelectListItem>();
}
}
Controller
publicclassHomeController : Controller
{
public ActionResult Create()
{
var countries = GetCountries();
var model = new CountryViewModel {AvailableCountries = countries};
return View(model);
}
[HttpPost]
publicasync Task<ActionResult> Create(CountryViewModel countryViewModel)
{
if (ModelState.IsValid)
{
int countryId = countryViewModel.SelectedCountryId;
// Do something
}
// If we got this far, something failed. So, redisplay form
countryViewModel.AvailableCountries = GetCountries();
return View(countryViewModel);
}
public IList<SelectListItem> GetCountries()
{
// This comes from database.var _dbCountries = new List<Country>
{
new Country {Country_id = 1, Description = "USA"},
new Country {Country_id = 2, Description = "UK"},
new Country {Country_id = 3, Description = "Canada"},
};
var countries = _dbCountries
.Select(x => new SelectListItem {Text = x.Description, Value = x.Country_id.ToString()})
.ToList();
countries.Insert(0, new SelectListItem {Text = "Choose a Country", Value = ""});
return countries;
}
}
View
@model DemoMvc.Models.CountryViewModel
@{
Layout = null;
}
<!DOCTYPE html><html><head><metaname="viewport"content="width=device-width" /><title>Create</title></head><body><h2>Create</h2>
@using (Html.BeginForm())
{
<divclass="form-group">
@Html.LabelFor(model => model.SelectedCountryId,
new {@class = "control-label col-md-2"})
<divclass="col-md-10">
@Html.DropDownListFor(model => model.SelectedCountryId,
Model.AvailableCountries, new {@class = "form-control"})
@Html.ValidationMessageFor(model => model.SelectedCountryId,
"", new {@class = "text-danger"})
</div></div><inputtype="submit"value="Submit"/>
}
</body></html>
Solution 2:
For .net core instead of @Html.ValidationMessageFor(...
Use
razor cshtml
<spanasp-validation-for="SelectedCountryId"class="text-danger"></span>
model poco
[Required(ErrorMessage = "Country is required.")]
publicint SelectedCountryId { get; set; }
Solution 3:
Model Class
[Display(Name = "Program")]
[Required, Range(1, int.MaxValue, ErrorMessage = "Select Program")]
publicstring Programid { get; set;
View
@Html.DropDownListFor(Model=>Model.Programid,(IEnumerable<SelectListItem>)ViewBag.Program, new {@id = "ddlProgram"})
Post a Comment for "How To Create Asp.net Mvc Dropdownlist With Required Validation"