As mentioned in the comments, you'll need to fix your duplicate IDs, and use
data-
attributes.
View:
<div class="row">
@foreach (var item in Model.Product)
{
<div class="col-lg-3 col-md-6">
<div class="panel panel-green" style=" border-radius: 25px; border: 1px solid #47d7ac;">
<div class="panel-heading">
<div class="row">
<div class="col-xs-12 ">
<h4 class="text-center">@item.Name</h4>
<div class="img-responsive">
<img src="@Url.Content(item.ImageUrl)" class="img-responsive img-thumbnail" />
</div>
</div>
</div>
</div>
<div class="panel-footer" style="background-color:transparent">
<div class="row">
<p class="text-center">@item.Description</p>
</div>
<div class="row">
<div class="col-md-6">
<h4 class="pull-left">₹ <span class="Cost">@item.Cost</span></h4>
</div>
<div class="col-md-6">
<input type="submit" class="btn btn-primary regbtn center-block -js-buy-button" style="width:auto" value="Buy" data-name="@item.Name" data-description="@item.Description" data-cost="@item.Cost" data-image="@item.ImageUrl" />
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
}
</div>
Script:
$(document).ready(function () {
$(document).on("click", ".-js-buy-button", function(event){
event.preventDefault();
var me = $(this);
var name = me.data("name");
var description = me.data("description");
var cost = me.data("cost");
var image = me.data("image");
console.debug("Name", name, "Description", description, "Cost", cost, "Image", image);
$.ajax({
url: '@Url.Action("GetOrder", "Home")',
data: { "Name": name, "Desc": description, "Cost": cost, "ImgUrl": image },
success: function(data){
window.location.href = data;
}
});
});
});
HOWEVER, this seems like a bad idea. As it stands, the user can simply use their browser's developer tools to change the price of the product before clicking the "Buy" button, and it looks like your code will simply accept the new price without validating it.
You're also not passing the product ID, so I suspect your database structure is broken. Instead of copying the product details to the order line, you should be using a foreign key relationship between the order line and product tables.
You should pass just the product ID to the server code. Your code can then use that ID to retrieve any details it needs from the products table.