asp.net mvc - MVC Required Validation not working -


my model :-

public class instrument {     [display(name = "id")]     public string id { get; set; }      [display(name = "instrument id")]     public string instrumentid     {         get;         set;     }            [display(name = "manufacturer"),required(allowemptystrings=false)]     public list<string> manufacturer     {         get;         set;     }  .....  ..... } 

my view has these

        <div class="form-group">             @html.labelfor(model => model.manufacturer[0], htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.manufacturer[0], new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.manufacturer[0], "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.manufacturer[1], htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.manufacturer[1], new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.manufacturer[1], "", new { @class = "text-danger" })             </div>         </div> 

when submit form controller (with out putting value in both these textboxes) list manufacturer contains 2 empty strings. want validate these. model.isvalid comes true because manufacturer has 2 empty strings.

you can use required attributes validate model [display(name = "id")] displaying id properties in model


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -