c# - id is null in mvc4 controller on form postback -


i'm trying send cart controller values when i'm trying access them in cart controller null in controller, although have them in hidden field in view correct value. happening when i'm trying add item cart. have:

razor view:

@using solution.order.models @model ienumerable<categoriesviewmodel>  @{     viewbag.title = "categories"; } @using (html.beginform()) {        <div class="panel-group">                        @foreach (var m in model)     {         <div class="panel panel-default">             <div class="panel-heading">                 <h4 class="panel-title">                     <a data-toggle="collapse" id="#accordion" data-parent="#accordion" href="#@m.categories.id.tostring()">                         @m.categories.name <span class="badge">@m.categories.itemcount</span>                     </a>                                      </h4>             </div>             <div id="@m.categories.id.tostring()" class="panel-collapse collapse">                                 <div class="panel-body">                     <ul class="list-group">                         @foreach (var in m.items)                         {                                                         <li class="list-group-item">                                 @i.name | price £@i.price                                   @using (html.beginform("addtocart", "cart"))                                 {                                     <div class="pull-right">                                         @html.hiddenfor(k => @i.id)                                         @html.hidden("returnurl", request.url.pathandquery)                                         <input type="submit" class="btn btn-success" value="add"/>                                     </div>                                 }                             </li>                                                     }                     </ul>                   </div>             </div>         </div>          }         </div>                                   }  <script>     $(document).ready(function() {         $('#accordion').on('click', function() {             $('#accordion .in').collapse('hide');         });     }); </script> 

controller content:

public redirecttorouteresult addtocart(guid? id, string returnurl)     {         restitem itemsingle = restitem.items.firstordefault(p => p.id == id);         if (itemsingle != null)         {             getcart().additem(itemsingle, 1);         }         return redirecttoaction("index", new {returnurl});     } 

in controller, id value null, although when view source of page i'm able see hidden value id:

enter image description here

since i'm still trying find best ways develop in mvc, approach i'm taking might not best one, every comment appreciated. in advance, laziale

the input field have in form having name "i.id" , action method expects parameter name id.mvc model binding expects have same name attribute values parameter name.

you may explicitly keep hidden field matching name

@foreach (var m in model) {   <h4>items</h4>   <ul>   @foreach(var in m.items)   {    <li>      <h4>@i.name | price @i.price</h4>      @using(html.beginform())      {        <div>            <input type="submit" class="btn btn-success" value="add"/>           @html.hidden("returnurl", request.url.pathandquery)              <input type="hidden" name="id" value="@m.id" />            </div>      }     </li>   } } 

this should work, assuming id property of item of type guid.

also, outer form tag nolonger being used. may remove it. without seeing viewmodel class, won't able doing wrong /rite.

few suggestions

  • if showing many items in loop, consider using editortemplates.
  • in specific case, prefer avoid multiple form tags in razor view , catch submit button click event in javascript , read hidden variable values (for returnurl , itemid) , redirect user new action method (since not worried keeping action method httppost attribute.

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -