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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -