I have an inventory application in a layout like this:
<body>
<div class="container" style="width: 100%">
<div id="header"> blahblahblah </div>
<div class="row">
<div id="rendermenu" class="col-sm-2">
@Html.Action("Menu", "Nav")
</div>
<div id="renderbody" class="col-sm-10">
@RenderBody()
</div>
</div>
</div>
In the Home
controller there is an action method that filters the repository sent to the view:
public ActionResult Index(string categoria, string marca)
{
InventarioViewModel viewModel = new InventarioViewModel
{
Inventario = repository.GetInventario()
.Where(i => (categoria == null || i.Item.Categoria == categoria)
&& (marca == null || i.Item.Marca == marca))
.OrderBy(i => i.Qta > 0)
.ThenBy(i => i.Item.Categoria)
.ThenBy(i => i.Item.Marca)
.ThenBy(i => i.Item.Modello).ToList(),
CategoriaCorrente = categoria,
MarcaCorrente = marca
};
return View(viewModel);
}
It works fine, but only by typing the desired route in the browser. So I started writing a filter by putting a <select>
element that presents the user the items categories, then binding an AJAX call to its change
event. This is the markup for the menu view:
@using Magazzino.Domain.Infrastructure
@using Magazzino.Domain.Entities
@model List<Item>
@{ IEnumerable<Item> items = Model.DistinctBy(x => x.Categoria); }
<div class="text-center">Filtra per:</div>
<hr />
<div class="text-center">Categoria</div>
<select class="form-control lista-categorie" id="lista-categorie" data-action-url="@Url.Action("Index", "Home")" name="categoria">
<option value>-- seleziona --</option>
@foreach (Item item in items)
{
<option value="@item.Categoria">@item.Categoria</option>
}
</select>
This is the AJAX call, put inside the usual $(document).ready(...):
$('#lista-categorie').on('change', function () {
var e = $(this)[0];
if (e.selectedIndex > 0) {
var ajaxRequest = $.ajax({
url: "/",
type: "get",
data: {
categoria: e.options[e.selectedIndex].value,
marca: null
}
})
ajaxRequest.done(function (response) {
$('#renderbody').html(response);
});
}
});
I also changed part of the controller where the view is rendered:
if (Request.IsAjaxRequest())
{
return PartialView(viewModel);
}
else
{
return View(viewModel);
}
but I get just an empty string. If try to get the full view, I see that the response has the whole page (layout and menu) BUT the Index view:
// previous markups from layout and menu
<div id="renderbody" class="col-sm-10">
</div>
</div>
</div>
// other markups
I'm sure the Index action method is triggered because I put a breakpoint. Though, the moment I change the AJAX call itself this way, it works fine:
var ajaxRequest = $.ajax({
url: "/" + e.options[e.selectedIndex].value,
type: "get"
})
but I don't really like this workaround, I'd like to have a more plain, efficient way of passing data to the controller.
How can I change this behaviour?
答案 0 :(得分:1)
总共,我的错:我重新编辑了该问题,因为当我说“我确定ViewModel已填充”时,它是真实的。我只检查了动作方法是否被触发。
我现在检查了ViewModel并发现了问题:我没有考虑到AJAX调用发送的“空”值实际上不是null
,而是空字符串< / strong>。我已经相应地更改了LINQ表达式,并且可以正常工作
InventarioViewModel viewModel = new InventarioViewModel
{
Inventario = repository.GetInventario()
.Where(i => (string.IsNullOrEmpty(categoria) || i.Item.Categoria == categoria)
&& (string.IsNullOrEmpty(marca) || i.Item.Marca == marca))
.OrderBy(i => i.Qta > 0)
.ThenBy(i => i.Item.Categoria)
.ThenBy(i => i.Item.Marca)
.ThenBy(i => i.Item.Modello).ToList(),
CategoriaCorrente = categoria,
MarcaCorrente = marca
};
尽管是一个琐碎的错误(以及令人难以置信的尴尬时刻),我还是想回答我自己的问题,并保留给那些可能遇到相同问题的人。
干杯。
答案 1 :(得分:0)
$('#lista-categorie').on('change',method_attached_to_event);
function method_attached_to_event(){
var e = $(this)[0];
if (e.selectedIndex > 0) {
var url = '/';
var obj = {};
obj.categoria= e.options[e.selectedIndex].value;
$.ajax({
url:url,
method:'POST',
data:obj,
success:success,
error:error
});
}else{
// Do something else
}
}
function success(data,status,jqxhr){
console.log(data);
}
function error( jqxhr, status, error ){
console.log(status);
console.log(error);
console.log(jqxhr);
}