asp.net-mvcmodel-view-controllermodelstateasp.net-mvc-validationasp.net-mvc-ajax

Html.ValidationMessageFor( MyKey ) without being tiedto a model's attribute?


My model does not really represent what my form is posting. Example my Orgs Model which holds orgs helps me generate a treeview the users selects several nodes of the orgs tree and submits a form. The form posts an array[] or org ids.

(maybe i'm doing this all wrong, please let me know tried binding to models and that was confusing when dealing with trees grids etc and using partial views and ajax returning partial views and editorfor's etc.. the default model binding was useless)

anyways back to my point, since I want to validate if any orgs get selected:

if (SelectedOrgs == null) //array[]
{
    ModelState.AddModelError("OrgsNotSelected",IValidationErrors.OrgsNotSelected);
}

my question is how do i retrieve this random key that i just made up from my view? my model and even my viewmodel do not have an array for the selection this is just the result of the post.

I'm not sure what to do in the view to get the value for "OrgsNotSelected".

Thank you! Bilal


Solution

  • If you were doing normal submit actions to your controller, you would need to use the ValidationSummary to display errors that are not attached to a specific property.

    As you are using Ajax, you would be better off returning a json result from your controller and you can define this so that it includes your errors in a format you can use in the success function to display your messages.