Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am developing shopping cart website in MVC3 with razor.below code is view code,in which i am showing multiple products,each product has quantity text box and one submit button.So,in controller how would i know which submit button is called by user and how to read quantity from textbox,because all submit button and textbox has same id because they are created dynamically.



C#
@using (Ajax.BeginForm("addtocart", "Chocolatier",FormMethod.Post,null))
 {

<table>
    <tr>

   @foreach (System.Data.DataRow i in Model.Rows )
   {
       <tr><td>  <img src=" @Url.Content("~/Content/abc.gif") "  alt="Imge" height ="40" width ="40"/></td></tr>
          foreach (System.Data.DataColumn j in Model.Columns)
          {
             <tr> <td style="width : 200px"><h5>@j.ToString(): </h5>@i[j]</td>
             </tr>
          }

           <td>Enter quantity : <input id="txtqty" type ="text" style="width : 50px" /></td>
     <tr><td><input type ="submit" value="Add to cart" /></td></tr>

       <tr>     <td>__________________________________________________________________________</td></tr>


                   }

    </tr>
</table>
Posted
Updated 18-Dec-13 20:07pm
v2

First of all it is very bad to have html elements with same ID, simply not accepted!!!
In your case, you can make the id distinct by easily appending the row index... and same can be done with submit button... Now when you have a submit click you can take the id of control causing form submit, which will give you the row index. Based on this you can easily get the corresponding text box...

There can be multiple solutions to this scenario. But please avoid duplicate ID's in any case.
 
Share this answer
 
v2
You need to change logic of the code hare.

With The same code
And Keep in mind answer of Solution 1 also

You can put the button on view as follows

HTML
<input type="submit" name = "action"   value="Add to cart (Product Name)"   />



and In the controller You can catch the name of the button

C#
string StrAcctinName = Request["action"].ToString();


and by using case statement or if statement you can call you code block.

But above is not perfect solution.
so you can do as follows

JavaScript
<script type="text/javascript">
    function Save(id , qty) {
       
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: "{id:'" + id + "',qty:'" + qty + "'}",
            url: '@Url.Action("addtocart", "Chocolatier")',
            dataType: "json",
            async: true,
           
            success: function (req) {
               message or scrpt
            },
            error: function (req, status, error) {
                alert(req.d);
		alert(status);
		alert(error);
            }

        });

       
    }
</script>


HTML
<table>
    <tr>

   @foreach (System.Data.DataRow i in Model.Rows )
   {
       <tr><td>  </td></tr>
          foreach (System.Data.DataColumn j in Model.Columns)
          {
             <tr> <td style="width : 200px"><h5>@j.ToString(): </h5>@i[j]</td>
             </tr>
          }

           <td>Enter quantity : <input id="txtqty" type="text" style="width : 50px" /></td>
     <tr><td> <input type="button"  önclick="Save(" + j.id + " ," + j.qty ")" value="Add to cart"> </input></td></tr>

       <tr>     <td>__________________________________________________________________________</td></tr>


                   }

    </tr>
</table>



You can check following link also .....

http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900