我有查看,部分视图列表。如何使用Ajax将模型发送到控制器,而不使用表单?
<script src="~/Scripts/SortProducts.js" type="text/javascript"></script>
@using System.Collections.Generic;
@using OnlineShop.Models;
@model List<Product>
<div>
<select id="sortList">
<option selected="selected" value="Sort products">Sort products</option>
<option value="from cheap to expensive">from cheap to expensive</option>
<option value="from expensive to cheap">from expensive to cheap</option>
<option value="Newest">Newest</option>
<option value="Oldest">Oldest</option>
</select>
</div>
<div>
<ul style="list-style-type:none">
@foreach (Product prod in Model)
{
<li >
@Html.Action("showOneProduct", "ShowProducts", new { product=prod })
<br/>
</li>
}
</ul>
</div>
当我选择在下拉列表元素,我想送模型利用ajax.Something这样给控制器。
$('#sortList').change(function() {
alert("SortProducts work");
$.ajax({
url: '/ShowProducts/sortProductsByFilter',
type: 'POST',
data: ???,
success: function (partialView) {
$('#showProduct').html(partialView);
$('#showProduct').show(partialView);
},
error: function() {
alert("SortProducts doesn't work");
}
})
})
我能做到这一点阿贾克斯,还是有更好的方法吗?如果不使用形式,我试图找到解决方案,但在网上,只有使用表单解决方案。 这里是我想要发送模型的Action方法的代码。
[HttpPost]
public ActionResult sortProductsByFilter(List<Product> products,string sortChoice)
{
return PartialView();
}
其中products
model
是是sortChoice从选择$('#sortList').val()
即选择SOR选项。
你想要什么样的模式来送?模型项目中的每个产品?你想用控制器中的值来做什么? – Shyju
的类型应该是POST,你需要Ø指定的内容类型 –
向我们展示了'/展示Products/sortProductsByFilter'控制器的动作,或者至少我要发的产品列表中的模型 – CodeNotFound