asp.net mvc - Adding a specific error message to a view in mvc -
is possible add specific error message field in view? have validation on model check whether field empty (validationmessagefor<>) , using validation summary specific errors. i'm wondering whether specific error message created summary can replace validationmessagefor<>?
model:
[required] public string schoolcode { get; set; }
view:
@html.validationsummary("", new { @class = "text-danger" }) @html.editorfor(m => m.schoolcode, new { htmlattributes = new { @class = "form-control", @placeholder = "school code" } }) @html.validationmessagefor(model => model.schoolcode, "", new { @class = "text-danger" })
controller:
var schoolexists = repositoryhelper.getschoolbycode(model.schoolcode); if (schoolexists == null) { modelstate.addmodelerror(string.empty, @"no school exists code"); return view(model); } if (modelstate.isvalid) { //do other stuff }
so if model state isn't valid returns model error, if incorrect code entered adds "no school exists code" validation summary. replace school code model error school code summary error?
you need specify property name in addmodelerror()
method
modelstate.addmodelerror("schoolcode", "no school exists code");
so displayed in placeholder identified @html.validationmessagefor(m => m.schoolcode, ... })
Comments
Post a Comment