Monday, 15 July 2013

asp.net mvc - TryValidateModel not assigning object, returning all nulls, MVCStore Example -



asp.net mvc - TryValidateModel not assigning object, returning all nulls, MVCStore Example -

in checkout controller have code

[httppost] public actionresult addressandpayment(formcollection values) { var order = new order(); tryvalidatemodel(order); ....

the model looks

[bind(exclude="orderid")] public partial class order { [scaffoldcolumn(false)] public int orderid { get; set; } [scaffoldcolumn(false)] public string username { get; set; } [required(errormessage= "first name required")] [displayname("first name")] [stringlength(160)] public string firstname { get; set; } [required(errormessage = "last name required")] [displayname("last name")] [stringlength(160)] public string lastname { get; set; } [required(errormessage="address required")] [stringlength(70)] public string address { get; set; } [required(errormessage = "city required")] [stringlength(40)] public string city { get; set; } [required(errormessage = "state required")] [stringlength(40)] public string state { get; set; } [required(errormessage = "postal code required")] [displayname("postal code")] [stringlength(10)] public string postalcode { get; set; } [required(errormessage="country required")] [stringlength(40)] public string country { get; set; } [required(errormessage= "phone required")] [stringlength(24)] public string phone { get; set; } [required(errormessage="email address required")] [displayname("email address")] [regularexpression(@"[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\.[a-za-z]{2,4}", errormessage="email not valid.")] [datatype(datatype.emailaddress)] public string email { get; set; } [scaffoldcolumn(false)] public decimal total { get; set; } [scaffoldcolumn(false)] public datetime orderdate { get; set; } public list<orderdetail> orderdetails { get; set; } }

i can stop right before tryvalidatemodel line , @ form values

? request.form["firstname"] "michael" ? values["firstname"] "michael"

so why tryvalidatemodel(order); homecoming false , order object not populated?

update clarify question know false means can not bind not know why can not bind. or should through tryvalidatemodel(or validatemodel)

but interesting if alter method signature

public actionresult addressandpayment(order order)

order gets populated correctly. if able bind in method phone call why not tryvalidatemodel(or validatemodel)?

i using mvc 4

tryvalidatemodel returns false when validation of form model against orders model fails, binding fails.

i hate using

tryvalidatemodel(order);

and prefer

validatemodel(order);

early on while developing page, because binding delicate process. way, if model fails bind, exception , indicative error msg.

asp.net-mvc asp.net-mvc-4 data-annotations

No comments:

Post a Comment