我有查看,部分視圖列表。如何使用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