Asp .net MVC Dropdownlist validation not working
Asp .net MVC Dropdownlist validation not working
Here is my Code, Am in learning stage please some one help me. Have three issue 1)validation not working. 2)In controller side modelState is invalid. 3) How to reset dropdownlist after submit.
public List<mstrClient> getCountry()
{
mstrClient objClientList = new mstrClient();
DataSet ds = new DataSet();
string sQuery = "SELECT * FROM INV_mstrCountry WHERE CStatus = 'Y'";
SqlDataAdapter da = new SqlDataAdapter(sQuery, con);
con.Open();
da.Fill(ds);
con.Close();
List<mstrClient> CountryList = new List<mstrClient>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
mstrClient objClient = new mstrClient();
objClient.CountryCode = ds.Tables[0].Rows[i]["CountryCode"].ToString();
objClient.Country = ds.Tables[0].Rows[i]["CountryName"].ToString();
CountryList.Add(objClient);
}
return CountryList;
}
public ActionResult Create()
{
mstrClient objClient = new mstrClient();
ModelState.Clear();
var List = getCountry();
ViewBag.Countrylist = new SelectList(List, "CountryCode", "Country");
return View(objClient);
}
In the View
You cannot get any client side validation using that overload of Presumably your You need to bind the dropdownlist to your property (and always use the string typed As a side note, you are editing data so you should always be using a view model, and that view model will contain a property for the and then the view becomes simply Refer also What is ViewModel in MVC?. In addition, you can remove the
@Html.LabelFor(model => model.CountryCode, htmlAttributes: new { @class = "control-label col-md-4" })
@Html.DropDownList("CountryList",null, "Select Country", htmlAttributes: new { @class = "form-control"})
@Html.ValidationMessageFor(model => model.CountryCode, "", new { @class = "text-danger" })
</div>
1 Answer
1
DropDownList()
. You are binding the selected value to a property named CountryList
which does not exist, but you are creating a label and validation message for a property named CountryCode
DropDownList()
CountryList
CountryCode
CountryCode
property has a [Required]
attribute and because you do not have a form control for it, its your model is invalid in the POST method. You then return the view, but because you are not binding to the model, the first option is again selected.CountryCode
[Required]
***For()
methods)***For()
@Html.DropDownListFor(m => m.CountryCode, (IEnumerable<SelectListItem>)ViewBag.CountryList, "Select Country", new { @class = "form-control" })
SelectList
SelectList
[Required]
public int CountryCode { get; set; }
public IEnumerable<SelectListItem> CountryList { get; set; }@Html.DropDownListFor(m => m.CountryCode, Model.CountryList, "Select Country", new { @class = "form-control" })
ModelState.Clear();
line of code - nothing has been added to ModelState
so its pointless.ModelState.Clear();
ModelState
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Its Working, Thank u so much Stephen.
– MohanKanal
2 days ago