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
<input type="submit" name = "action" value="Add to cart (Product Name)" />
and In the controller You can catch the name of the button
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
<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>
<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[
^]