Inside my controller's action I have the following code:
public ActionResult GridAction(string id)
{
if (String.IsNullOrEmpty(id))
{
// add errors to the errors collection and then return the view saying that you cannot select the dropdownlist value with the "Please Select" option
}
return View();
}
UPDATE:
if (String.IsNullOrEmpty(id))
{
// add error
ModelState.AddModelError("GridActionDropDownList", "Please select an option");
return RedirectToAction("Orders");
}
UPDATE 2:
Here is my updated code:
@Html.DropDownListFor(x => x.SelectedGridAction, Model.GridActions,"Please Select")
@Html.ValidationMessageFor(x => x.SelectedGridAction)
The Model looks like the following:
public class MyInvoicesViewModel
{
private List<SelectListItem> _gridActions;
public int CurrentGridAction { get; set; }
[Required(ErrorMessage = "Please select an option")]
public string SelectedGridAction { get; set; }
public List<SelectListItem> GridActions
{
get
{
_gridActions = new List<SelectListItem>();
_gridActions.Add(new SelectListItem() { Text = "Export to Excel", Value = "1" });
return _gridActions;
}
}
}
And here is my controller action:
public ActionResult GridAction(string id)
{
if (String.IsNullOrEmpty(id))
{
// add error
ModelState.AddModelError("SelectedGridAction", "Please select an option");
return RedirectToAction("Orders");
}
return View();
}
Nothing happens! I am totally lost on this one!
UPDATE 3:
I am now using the following code but still the validation is not firing:
public ActionResult GridAction(string id)
{
var myViewModel= new MyViewModel();
myViewModel.SelectedGridAction = id; // id is passed as null
if (!ModelState.IsValid)
{
return View("Orders");
}
UPDATE 4:
$("#linkGridAction").click(function () {
alert('link grid action clicked');
$.get('GridAction/', { SelectedGridAction: $("#SelectedGridAction").val() }, function (result) {
alert('success');
});
});
And the Controller looks like the following:
// OrderViewModel has a property called SelectedGridAction.
public ActionResult GridAction(OrderViewModel orderViewModel)
{
return View();
}
UPDATE 5: Validation is not firing:
public ActionResult GridAction(OrderViewModel orderViewModel)
{
if (!ModelState.IsValid)
{
return View("Orders", orderViewModel);
}
return View();
}
Above, we added a custom error message using the ModelState. AddModelError() method. The ValidationSummary() method will automatically display all the error messages added into the ModelState .
The ValidationSummary can be used to display all the error messages for all the fields. It can also be used to display custom error messages.
In order for the Validator 's error message to display in the ValidationSummary , you need to set the Validator s Display="none" .
Use ModelState.AddModelError()
ModelState.AddModelError("MyDropDownListKey", "Please Select");
and output to the view like this:
<%= Html.ValidationMessage("MyDropDownListKey") %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With