Jquery 如何将Html.TextBox值与控制器和Html.Action一起使用?
假设我有一个视图,其中包含任何用户都可以购买和订购的产品。所以在这个视图中有一个按钮:“添加到购物车”,产品描述,产品订单金额。在此视图中,用户可以通过Html.TextBox选择金额,然后单击“添加到购物车”进行订购。所以我一直在尝试编写代码,但我不确定代码是否正确(脚本是问题所在) 请注意,我的控制器函数有两个参数:产品本身(产品有一个模型)和数量(整数)。 嗯,我对这个产品没有任何问题,但一旦我开始使用它的数量,它就变得更加复杂了 这是我的代码附件:Jquery 如何将Html.TextBox值与控制器和Html.Action一起使用?,jquery,html,css,asp.net,asp.net-mvc,Jquery,Html,Css,Asp.net,Asp.net Mvc,假设我有一个视图,其中包含任何用户都可以购买和订购的产品。所以在这个视图中有一个按钮:“添加到购物车”,产品描述,产品订单金额。在此视图中,用户可以通过Html.TextBox选择金额,然后单击“添加到购物车”进行订购。所以我一直在尝试编写代码,但我不确定代码是否正确(脚本是问题所在) 请注意,我的控制器函数有两个参数:产品本身(产品有一个模型)和数量(整数)。 嗯,我对这个产品没有任何问题,但一旦我开始使用它的数量,它就变得更加复杂了 这是我的代码附件: @*@model SurffingSi
@*@model SurffingSite.Models.Products*@
@模型列表
@{
ViewBag.Title = "DisplayAllProducts";
}
我们的产品
@foreach(模型中的var乘积)
{
产品名称:@product.ProductName
类别:@product.Category
Description:@product.Description
价格:@product.Price₪
输入金额:
}
$(“.btn btn info btn lg”)。单击(函数(事件)
{
event.preventDefault();
var url='@url.Action(“AddToCart”、“Cart”、new{prod=“product”、amount=“theamounter”});
url=url.replace(“theamounter”,$(“#amounter”).val();
url=url.replace(“产品”,$(this.data());
window.location.href=url;
});
以下是相关的控制器方法:
这是我的产品对象模型:
如果您的视图强类型化为单个产品,那么您使用
foreach(模型中的var-Product)
line试图实现什么?并且您不需要发送整个产品对象。您只需要产品ID视图中有许多产品。。我只是想简化一下。。所以foreach代表展示所有的产品。。作为你的第二个评论-为什么我不需要整个对象?在我的控制器中,我确实使用了整个对象。你应该发布正确的代码!只发布相关部分是好的,但发布正确的代码。为什么在动作方法中需要整个对象?为什么不仅仅是productId?好的,我刚刚更新了。。
<div class="row">
@foreach (var product in Model)
{
<figure class="portfolio-item col-md-4 col-sm-6">
<img src="~/images/@product.Picture" class="img-responsive" />
<div class="product-details">
<h2 class="product-name">Products Name: @product.ProductName</h2>
<label>Category: @product.Category</label>
<div class="product-description">Description: @product.Description</div>
<div class="product-price">Price: @product.Price ₪</div>
<div class="form-group">
<label>Enter amount:</label>
<div class="col-md-10">
<input type="text" name="amounter" id="amounter" />
</div>
</div>
<div>
<a id="btn_add" href="" data-id ="@product" class="btn btn-info btn-lg" style="margin-left:60px">
<span class="glyphicon glyphicon-shopping-cart"></span> Add to cart
</a><br />
<a id="btn_more_info" href="@Url.Action("Index", "Home")" class="btn btn-info btn-lg" style="margin-left:60px">
<span class="glyphicon glyphicon-eye-open"></span> Read more
</a>
</div>
</div>
</figure>
}
</div>
</section>
<script>
$(".btn btn-info btn-lg").click(function(event)
{
event.preventDefault();
var url = '@Url.Action("AddToCart", "Cart", new {prod = "product", amount = "theamounter"})';
url = url.replace("theamounter", $("#amounter").val());
url = url.replace("product", $(this).data());
window.location.href = url;
});
</script>