Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add validation errors in the validation collection asp.net mvc?

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(); 
}
like image 661
johndoe Avatar asked Jan 13 '11 14:01

johndoe


People also ask

How do you add errors in ModelState?

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 .

Can we display all errors in one go in MVC?

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.

How do you show custom validator error message in validation summary?

In order for the Validator 's error message to display in the ValidationSummary , you need to set the Validator s Display="none" .


1 Answers

Use ModelState.AddModelError()

ModelState.AddModelError("MyDropDownListKey", "Please Select");

and output to the view like this:

<%= Html.ValidationMessage("MyDropDownListKey") %>
like image 89
hunter Avatar answered Nov 15 '22 21:11

hunter