为什么我在发送表单时没有得到 wright 值?
Why am I not getting the wright values when sending my form?
我是 .NET 新手,英语不好。
这是我的表单的代码。目前,我只获取 productName 的值,但我也需要获取“category”的值。调用控制器时,“category”保持为 Null。我究竟做错了什么?有人可以在这里给我提示吗?
<form asp-controller="Products" asp-action="ByCategory" method="get">
<div class="col" style="margin: 4px 10px 12px 1px">
<div class="input-group" mt-2>
<input type="text" name="productName" class="form-control" placeholder="Serch by name" />
<div class="input-group-append">
<input type="submit" class="btn btn-primary" value="Search" />
</div>
<button class="btn btn-primary dropdown-toggle" type="button" name="category"
id="category" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
style="margin: 0px 0px 0px 10px">
Categories
</button>
<div class="dropdown-menu" aria-labelledby="">
@for (int i = 0; i <= 2; i++)
{
<button class="dropdown-item" type="button">@ViewBag.Data[i]</button>
}
</div>
</div>
</div>
</form>
``````
and here is the code for my controller
``````
public async Task<IActionResult> ByCategory(string category, string productName)
{
var model = _context.Product.Where(p => p.Name.Equals(productName)).Select(p => new ProductViewModel
{
Id = p.Id,
Name = p.Name,
Price = p.Price,
Count = p.Count,
InventoryValue = p.Count * p.Price,
Category = p.Category
});
return View("ByCategory", await model.ToListAsync());
}
``````
目前您已经为表单定义了一个输入
<input type="text" name="productName" class="form-control" placeholder="Serch by name" />
您还应该为 category
定义第二个输入,这样它也将与表单一起提交。
1.Form提交不能自动post按钮值。您需要设置隐藏输入,并在选择下拉列表时使用jQuery设置输入值。
2.Model 绑定通过名称属性绑定 属性。您需要为隐藏输入设置 name="category"
。
这是一个完整的工作演示:
<form asp-controller="Products" asp-action="ByCategory" method="get">
<div class="col" style="margin: 4px 10px 12px 1px">
<div class="input-group" mt-2>
<input type="text" name="productName" class="form-control" placeholder="Serch by name" />
//add hidden input here..
<input type="text" name="category" id="category" class="form-control" hidden>
<div class="input-group-append">
<input type="submit" class="btn btn-primary" value="Search" />
</div>
<button class="btn btn-primary dropdown-toggle" type="button" name="category"
id="category" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
style="margin: 0px 0px 0px 10px">
Categories
</button>
<div class="dropdown-menu" aria-labelledby="">
@for (int i = 0; i <= 2; i++)
{
<button class="dropdown-item" type="button">@ViewBag.Data[i]</button>
}
</div>
</div>
</div>
</form>
@section Scripts
{
<script>
$("button.dropdown-item").click(function(){
$("#category").val($(this).text());
})
</script>
}
我是 .NET 新手,英语不好。 这是我的表单的代码。目前,我只获取 productName 的值,但我也需要获取“category”的值。调用控制器时,“category”保持为 Null。我究竟做错了什么?有人可以在这里给我提示吗?
<form asp-controller="Products" asp-action="ByCategory" method="get">
<div class="col" style="margin: 4px 10px 12px 1px">
<div class="input-group" mt-2>
<input type="text" name="productName" class="form-control" placeholder="Serch by name" />
<div class="input-group-append">
<input type="submit" class="btn btn-primary" value="Search" />
</div>
<button class="btn btn-primary dropdown-toggle" type="button" name="category"
id="category" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
style="margin: 0px 0px 0px 10px">
Categories
</button>
<div class="dropdown-menu" aria-labelledby="">
@for (int i = 0; i <= 2; i++)
{
<button class="dropdown-item" type="button">@ViewBag.Data[i]</button>
}
</div>
</div>
</div>
</form>
``````
and here is the code for my controller
``````
public async Task<IActionResult> ByCategory(string category, string productName)
{
var model = _context.Product.Where(p => p.Name.Equals(productName)).Select(p => new ProductViewModel
{
Id = p.Id,
Name = p.Name,
Price = p.Price,
Count = p.Count,
InventoryValue = p.Count * p.Price,
Category = p.Category
});
return View("ByCategory", await model.ToListAsync());
}
``````
目前您已经为表单定义了一个输入
<input type="text" name="productName" class="form-control" placeholder="Serch by name" />
您还应该为 category
定义第二个输入,这样它也将与表单一起提交。
1.Form提交不能自动post按钮值。您需要设置隐藏输入,并在选择下拉列表时使用jQuery设置输入值。
2.Model 绑定通过名称属性绑定 属性。您需要为隐藏输入设置 name="category"
。
这是一个完整的工作演示:
<form asp-controller="Products" asp-action="ByCategory" method="get">
<div class="col" style="margin: 4px 10px 12px 1px">
<div class="input-group" mt-2>
<input type="text" name="productName" class="form-control" placeholder="Serch by name" />
//add hidden input here..
<input type="text" name="category" id="category" class="form-control" hidden>
<div class="input-group-append">
<input type="submit" class="btn btn-primary" value="Search" />
</div>
<button class="btn btn-primary dropdown-toggle" type="button" name="category"
id="category" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
style="margin: 0px 0px 0px 10px">
Categories
</button>
<div class="dropdown-menu" aria-labelledby="">
@for (int i = 0; i <= 2; i++)
{
<button class="dropdown-item" type="button">@ViewBag.Data[i]</button>
}
</div>
</div>
</div>
</form>
@section Scripts
{
<script>
$("button.dropdown-item").click(function(){
$("#category").val($(this).text());
})
</script>
}