Brian Brian - 1 month ago 8
ASP.NET (C#) Question

DropDownList Validation

I'm getting a validation error message on my DropDownList within my Create Action if I click the submit button without selecting a value from the DropDownList.

The validation error that I'm getting is The value "" is invalid. I would like the validation error message to say State is required! instead.

I added the required validation attribute to the StId property on the ViewModel but that did not resolve the issue.

Customer Model

public class Customer
{
public int CustId { get; set; }
public string CustFirstName { get; set; }
public string CustLastName { get; set; }

public int StId { get; set; }
public State State { get; set; }
}


State Model

public class State
{
public int StId { get; set; }
public string StAbbr { get; set; }

public List<Customer> Customers { get; set; }
}


CustomerFormViewModel

public class CustomerFormViewModel
{
public int CustId { get; set; }

[Required(ErrorMessage = "First Name is required!")]
[Display(Name = "First Name")]
public string CustFirstName { get; set; }

[Required(ErrorMessage = "Last Name is required!")]
[Display(Name = "Last Name")]
public string CustLastName { get; set; }

[Required(ErrorMessage = "State is required!")]
[Display(Name = "State")]
public int StId { get; set; }

public IEnumerable<State> States { get; set; }
}


CustomerController

public class CustomerController : Controller
{
private OneClickAwayDbContext _context;

public CustomerController(OneClickAwayDbContext context)
{
_context = context;
}

public ActionResult Index()
{
return View(_context.Customers.ToList());
}

public ActionResult Create()
{
var states = _context.States.ToList();
var viewModel = new CustomerFormViewModel
{
States = states
};

return View(viewModel);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CustomerFormViewModel vm)
{
if (ModelState.IsValid)
{
var customer = new Customer();
{
customer.CustFirstName = vm.CustFirstName;
customer.CustLastName = vm.CustLastName;
customer.StId = vm.StId;
}

_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("Index");
}

else
{
var stateViewModel = new CustomerFormViewModel
{
States = _context.States.ToList()
};
return View("Create", stateViewModel);
}
}
}


Create.chtml

@using (Html.BeginForm("Create", "Customer"))
{
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustFirstName)
</div>

<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustLastName)
</div>

<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class="form-control"})
@Html.ValidationMessageFor(s => s.StId)
</div>

<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
}

Answer

Please include jquery.validate.js and jquery.validate.unobtrusive.js in your cshtml file

<script src="~/lib/jquery-validation/dist/jquery.validate.js"></scri‌​pt>     
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.uno‌​btrusive.js"></scrip‌​t>
Comments