asp.net mvc - C# How to initilization a variable in class? -


i'm creating billing address user shipping including (first name, last name, address 1, address 2...) based on mvc music store project

[httppost]     public actionresult addressandpayment(formcollection values)     {         var order = new salesorderheader();         var order1 = new salesorderdetail();         tryupdatemodel(order);         try         {             if (string.equals(values["promocode"], promocode, stringcomparison.ordinalignorecase) == false)             {                 return view(order);             }             else             {                 order.accountnumber = user.identity.name;                 order.orderdate = datetime.now;                 order.address.addressline1 = values["addressline1"];                 order.address.addressline2 = values["addressline2"];                 order.address.city = values["city"];                  //save order                 bikedbs.salesorderheaders.add(order);                 bikedbs.savechanges();                 //process order                  var cart = shoppingcart.getcart(this.httpcontext);                 cart.createorder(order);                 //cart.createorder(order1);                  return redirecttoaction("complete", new {id = order.salesorderid });             }         }         catch         {             //invalid - redisplay errors             return view(order);         } 

}

may have ask dump question? initial order.address variable? address string type. updated code following metioned. based on mvc music store project , created new app using adventurework2012 db. here view:

<h2>address , payment</h2>     <fieldset>         <legend>shipping information</legend>         @html.displaynamefor(model => model.customer.person.firstname): @html.textbox("firstname") <br />         @html.displaynamefor(model => model.customer.person.lastname):  @html.textbox("lastname") <br />         @html.displaynamefor(model => model.address.addressline1): @html.textbox("addressline1") <br />         @html.displaynamefor(model => model.address.addressline2): @html.textbox("addressline2") <br />         @html.displaynamefor(model => model.address.city): @html.textbox("city") <br />        </fieldset> 

and address.cs:

public partial class address     {         public address()         {             this.salesorderheaders = new hashset<salesorderheader>();             this.salesorderheaders1 = new hashset<salesorderheader>();         }          public int addressid { get; set; }         public string addressline1 { get; set; }         public string addressline2 { get; set; }         public string city { get; set; }         public int stateprovinceid { get; set; }         public string postalcode { get; set; }         public system.guid rowguid { get; set; }         public system.datetime modifieddate { get; set; }          public virtual stateprovince stateprovince { get; set; }         public virtual icollection<salesorderheader> salesorderheaders { get; set; }         public virtual icollection<salesorderheader> salesorderheaders1 { get; set; }     } 

in c# string null if not initialized.

in mvc music store project, can follow code:

the addressandpayment view shows shipping information has helper @html.editorformodel() , view model of type mvcmusicstore.models.order.

following class name see properties exposed in form.

and 1 of properties in fact address written as

[required(errormessage = "address required")] [stringlength(70)] public string address { get; set; } 

as action accepting formcollection instead of model, need do:

order.address = values["address"]; 

updated new code on question

there's 3 small mistakes code:

1 - code using tryupdatemodel , can't use complex types (your new address object complex object , not primitive 1 (string, int, etc))

2 - because of point 1, need comment out tryupdatemodel , instead, pass order input public actionresult addressandpayment(order order, formcollection values)

3 - in view, rendering new properties @html.textbox("addressline1") explicit name of <input> addressline1 witch it's wrong model, should address.addressline1.

to fix this, use helper editorfor , use like:

@html.displaynamefor(model => model.customer.person.firstname):  @html.editorfor(model => model.customer.person.firstname) <br />  @html.displaynamefor(model => model.customer.person.lastname):   @html.editorfor(model => model.customer.person.lastname) <br />  @html.displaynamefor(model => model.address.addressline1):  @html.editorfor(model => model.address.addressline1) <br />  @html.displaynamefor(model => model.address.addressline2):  @html.editorfor(model => model.address.addressline2) <br />  @html.displaynamefor(model => model.address.city):  @html.editorfor(model => model.address.city) <br /> 

this create <input> like

<input class="text-box single-line" id="address_addressline1" name="address.addressline1" type="text" value=""> 

instead 1 have:

<input id="addressline1" name="addressline1" type="text"> 

now, default action, work out of box, without need updated, when save order, have fields form in order object

    [httppost]     public actionresult addressandpayment(order order, formcollection values)     {         try         {             if (string.equals(values["promocode"], promocode,                 stringcomparison.ordinalignorecase) == false)             {                 return view(order);             }             else             {                 order.username = user.identity.name;                 order.orderdate = datetime.now;                  //save order                 storedb.orders.add(order);                 storedb.savechanges();                  //process order                 var cart = shoppingcart.getcart(this.httpcontext);                 cart.createorder(order);                  return redirecttoaction("complete",                     new { id = order.orderid });             }          }         catch         {             //invalid - redisplay errors             return view(order);         }     } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -