c# - Null reference exception after POST -


this question has answer here:

i have form, gets values model. then, in post data user entered handled , user redirected page. everytime post form, null-reference exeption. make mistake?

none of other questions asking didn´t solve problem, that´s why asking again specific code.

the exeptions @ foreach loops - model.cart, model.shippingoptionsm etc.

@model checkoutviewmodel @{ viewbag.title = "checkout"; layout = "~/views/shared/_layout.cshtml"; }  <h2>checkout</h2> <table class="table">     <thead>         <tr>             <td>#</td>             <td>name</td>             <td>quantity</td>             <td>price</td>             <td>total price</td>         </tr>     </thead>     @foreach (cartitem in model.cart)     {         <tr>             <td>@html.action("productposition", "cart", new { item = })</td>             <td>@i.name</td>             <td>@i.quantity</td>             <td>@i.price €</td>             <td>@i.total €</td>         </tr>     }  </table> <h3>total: @html.action("totalprice", "cart") €</h3>  @using (html.beginform("checkout", "cart")) {     @html.antiforgerytoken()     <h2>address</h2>     <div class="form-group">         @html.labelfor(m => m.name)         @html.textboxfor(m => m.name, new { @class = "form-control" })         @html.validationmessagefor(m => m.name)     </div>     <div class="form-group">         @html.labelfor(m => m.address)         @html.textboxfor(m => m.address, new { @class = "form-control" })         @html.validationmessagefor(m => m.address)     </div>     <div class="form-group">         @html.labelfor(m => m.city)         @html.textboxfor(m => m.city, new { @class = "form-control" })         @html.validationmessagefor(m => m.city)     </div>     <div class="form-group">         @html.labelfor(m => m.postnumber)         @html.textboxfor(m => m.postnumber, new { @class = "form-control" })         @html.validationmessagefor(m => m.postnumber)     </div>     <div class="form-group">         @html.labelfor(m => m.state)         @html.textboxfor(m => m.state, new { @class = "form-control" })         @html.validationmessagefor(m => m.state)     </div>        <h2>shipping</h2>     foreach (var in model.shippingoptions)     {         <div class="radio">             @html.radiobuttonfor(m => m.shippingoption, i.id) @i.name - @i.price €         </div>     }     @html.validationmessagefor(m => m.shippingoption);      <h2>payment</h2>     foreach (var in model.paymentoptions)     {         <div class="radio">             @html.radiobuttonfor(m => m.paymentoption, i.id) @i.name         </div>     }     @html.validationmessagefor(m => m.paymentoption);     <button type="submit" class="btn btn-primary">continue</button> } @section scripts {     @scripts.render("~/bundles/jqueryval") } 

controller:

public actionresult checkout()     {         if (session["cart"] == null)             return redirecttoaction("index");          var checkout = new checkoutviewmodel()         {             cart = (list<cartitem>)session["cart"],             paymentoptions = _context.paymentoptions.tolist(),             shippingoptions = _context.shippingoptions.tolist()         };           return view(checkout);     }      [httppost]     [validateantiforgerytoken]     public actionresult checkout(checkoutviewmodel m)     {         if (!modelstate.isvalid)             return view();          //todo: handle data          return redirecttoaction("ordersuccess");     } 

add model declaration @ top of page

@model checkoutviewmodel 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -