asp.net mvc - Invalid model state in Posting data with form in MVC5 -


i'm new in asp.net mvc

i want fill form , post data base model binder validation false . , errors have in model doesn't show

i'm sorry because don't know problem couldn't shorten it:

here model:

public class request {     //pkey     public virtual int id { get; set; }     //fkey     public virtual int tourid { get; set; }      [required]     [maxlength(150, errormessageresourcetype = typeof(errorresource), errormessageresourcename = "checklenght")]     public virtual string firstname { get; set; }       [required]     [maxlength(150, errormessageresourcetype = typeof(errorresource), errormessageresourcename = "checklenght")]     public virtual string lastname { get; set; }      [required]     [emailaddress(errormessageresourcetype = typeof(errorresource), errormessageresourcename = "email")]     [maxlength(150, errormessageresourcetype = typeof(errorresource), errormessageresourcename = "checklenght")]     public virtual string email { get; set; }      [required]     public virtual string phone { get; set; }      [maxlength(100000000, errormessageresourcetype = typeof(errorresource), errormessageresourcename = "checklenght")]     public virtual string comment { get; set; }       public virtual bool frequenttraveler { get; set; }      [required]     [range(1, 500000)]     public virtual int travelerscount { get; set; }       [datatype(datatype.date)]     [displayformat(dataformatstring = "{0:dd/mm/yyyy}")]     public virtual string date { get; set; }       public virtual bool contacttimepreference { get; set; }      [maxlength(150, errormessageresourcetype = typeof(errorresource), errormessageresourcename = "checklenght")]     public virtual string country { get; set; }       public virtual bool archived { get; set; } 

and form in view :

@using (html.beginform("create", "request")) {   <div class="form-group">    <input  type="hidden" name="tourid" value="4"/>   </div>   <div class="form-group">  @html.editorfor(model => model.request.firstname, new { htmlattributes = new { @class = "form-control" } })  @html.validationmessagefor(model => model.request.firstname, "", new { @class = "text-danger" , placeholder = "firstname" })   </div>    <div class="form-group">  @html.editorfor(model => model.request.lastname, new { htmlattributes = new { @class = "form-control" } })  @html.validationmessagefor(model => model.request.lastname, "", new { @class = "text-danger" }) </div>   <div class="form-group">                                        @html.editorfor(model => model.request.email, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.request.email, "", new { @class = "text-danger" }) </div>  <div class="form-group">  @html.editorfor(model => model.request.phone, new { htmlattributes = new { @class = "form-control" } })  @html.validationmessagefor(model => model.request.phone, "", new { @class = "text-danger" }) </div>    <div class="form-group ft">  @html.editorfor(model => model.request.frequenttraveler)  @html.validationmessagefor(model => model.request.frequenttraveler, "", new { @class = "text-danger" })     </div>   <div class="form-group">    <input type="hidden" name="travelerscount" value="3" /> </div>    <div class="form-group">     <input type="hidden" name="travelerscount" value="3" />   </div>    } 

i omit of form groups allow null shorthand.

and create action in request controller:

 [httppost]     [validateantiforgerytoken]     public actionresult create([bind(include = "id,tourid,firstname,lastname,email,phone,comment,frequenttraveler,travelerscount,date,contacttimepreference,country,archived")] request request)     {         if (modelstate.isvalid)         {             db.requests.add(request);             db.savechanges();             return redirecttoaction("index");         }          return view();     } 

i appreciate if 1 tell me problem make request object valid.and if possible send errors user when example put null in first name required server.

modelstate invalid because view generating form controls model different model expect in post method. example

@html.editorfor(model => model.request.firstname, ...) 

is generating

<input type="text" name="request.firstname" .... /> 

but model in post method request not have property named request - has 1 named firstname binding fails , modelstate invalid because of [required] validation attribute.

in order bind request model, html need be

<input type="text" name="firstname" .... /> 

in addition, [bind] attribute excluding values correctly binding since include "request".

remove [bind] attribute (you seem wanting bind anyway default) , either change model in post method match model declared in view, or use prefix property of bindattribute

public actionresult create([bind(prefix= "request")] request model) 

and delete manually html hidden inputs , use @html.hiddenfor(m => m.request.tourid) etc name attributes consistent , correctly bind.

finally, recommend read what viewmodel in mvc?. using data models in views, particularly when editing forms poor practice.

side note: can use following query in post methods check properties of model have validation errors

var errors = modelstate.keys.where(k => modelstate[k].errors.count > 0).select(k => new {     propertyname = k,     errormessage = modelstate[k].errors[0].errormessage }); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -