使用参数提交表单到操作,Asp.Net.Core Mvc
Submit form to action with parameters, Asp.Net.Core Mvc
我正在尝试通过 asp.net.core 中的 ajax 提交排序下拉表单。动作还有其他参数,我也想在动作中使用这些参数,但表单不提交其他参数。
我认为,主要问题是剃须刀中的表单部分
这是我行动的一部分:
public IActionResult Products( int? page, int? categoryId, string searchName = "", string
sortOrder="")
{
int pageSize = 1;
int pageNumber = (page ?? 1);
ViewBag.SearchName = searchName;
ViewBag.CategoryId = categoryId;
ViewBag.SortOrder = sortOrder;
........
model.ProductInfo = productList.ToList().ToPagedList(pageNumber,
pageSize);
if (isAjax)
return
PartialView("/Views/Shared/FrontPartialViews/_FrontProductList.cshtml",
model.ProductInfo);
else
return View("/Views/User/Products.cshtml", model);
}
这是表格:
<form asp-controller="User" asp-action="Products"
asp-route- categoryId="@ViewBag.CategoryId"
asp-route-searchName="@ViewBag.searchName"
data-ajax="true"
data-ajax-method="Get"
data-ajax-update="product"
data_ajax_mode="replace">
<select name="sortOrder" class="form-control" onchange="orderByChanged()">
<option selected> Please select one</option>
<option value="date_desc">
date_desc
</option
<select>
</form>
在 data-ajax-url
中提供操作 url:
<form method="get"
data-ajax="true"
data-ajax-method="get"
data-ajax-update="product"
data-ajax-mode="replace"
data-ajax-url="@Url.Action("Products", "User")">
<input type="hidden" name="categoryId" value="ViewBag.CategoryId" />
<input type="hidden" name="searchName" value="ViewBag.SearchName" />
<select name="sortOrder" class="form-control" onchange="orderByChanged()">
<option selected> Please select one</option>
<option value="date_desc">date_desc</option>
<select>
</form>
这是一个像下面这样的工作演示:
视图(Index.cshtml):
<form asp-controller="User" asp-action="Products"
asp-route-categoryId="@ViewBag.CategoryId"
asp-route-searchName="@ViewBag.SearchName"
data-ajax="true"
data-ajax-method="Get"
data-ajax-update="product"
data_ajax_mode="replace">
<select name="sortOrder" class="form-control" onchange="orderByChanged()">
<option selected> Please select one</option>
<option value="date_desc">
date_desc
</option>
</select>
<input type="submit" value="Submit" class="btn btn-primary" />
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ajax-unobtrusive/3.2.6/jquery.unobtrusive-ajax.js" integrity="sha256-v2nySZafnswY87um3ymbg7p9f766IQspC5oqaqZVX2c=" crossorigin="anonymous"></script>
<script>
function orderByChanged() {
$('form').submit();
}
</script>
}
控制器:
请务必在首次渲染视图时设置 ViewBag.CategoryId
和 ViewBag.SearchName
。
public IActionResult Products(int? page, int? categoryId, string searchName = "", string sortOrder = "")
{
int pageSize = 1;
int pageNumber = (page ?? 1);
ViewBag.SearchName = searchName;
ViewBag.CategoryId = categoryId;
ViewBag.SortOrder = sortOrder;
//....
}
public async Task<IActionResult> Index()
{
ViewBag.CategoryId = 1;
ViewBag.SearchName = "aaa";
return View();
}
结果:
我正在尝试通过 asp.net.core 中的 ajax 提交排序下拉表单。动作还有其他参数,我也想在动作中使用这些参数,但表单不提交其他参数。 我认为,主要问题是剃须刀中的表单部分
这是我行动的一部分:
public IActionResult Products( int? page, int? categoryId, string searchName = "", string
sortOrder="")
{
int pageSize = 1;
int pageNumber = (page ?? 1);
ViewBag.SearchName = searchName;
ViewBag.CategoryId = categoryId;
ViewBag.SortOrder = sortOrder;
........
model.ProductInfo = productList.ToList().ToPagedList(pageNumber,
pageSize);
if (isAjax)
return
PartialView("/Views/Shared/FrontPartialViews/_FrontProductList.cshtml",
model.ProductInfo);
else
return View("/Views/User/Products.cshtml", model);
}
这是表格:
<form asp-controller="User" asp-action="Products"
asp-route- categoryId="@ViewBag.CategoryId"
asp-route-searchName="@ViewBag.searchName"
data-ajax="true"
data-ajax-method="Get"
data-ajax-update="product"
data_ajax_mode="replace">
<select name="sortOrder" class="form-control" onchange="orderByChanged()">
<option selected> Please select one</option>
<option value="date_desc">
date_desc
</option
<select>
</form>
在 data-ajax-url
中提供操作 url:
<form method="get"
data-ajax="true"
data-ajax-method="get"
data-ajax-update="product"
data-ajax-mode="replace"
data-ajax-url="@Url.Action("Products", "User")">
<input type="hidden" name="categoryId" value="ViewBag.CategoryId" />
<input type="hidden" name="searchName" value="ViewBag.SearchName" />
<select name="sortOrder" class="form-control" onchange="orderByChanged()">
<option selected> Please select one</option>
<option value="date_desc">date_desc</option>
<select>
</form>
这是一个像下面这样的工作演示:
视图(Index.cshtml):
<form asp-controller="User" asp-action="Products"
asp-route-categoryId="@ViewBag.CategoryId"
asp-route-searchName="@ViewBag.SearchName"
data-ajax="true"
data-ajax-method="Get"
data-ajax-update="product"
data_ajax_mode="replace">
<select name="sortOrder" class="form-control" onchange="orderByChanged()">
<option selected> Please select one</option>
<option value="date_desc">
date_desc
</option>
</select>
<input type="submit" value="Submit" class="btn btn-primary" />
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ajax-unobtrusive/3.2.6/jquery.unobtrusive-ajax.js" integrity="sha256-v2nySZafnswY87um3ymbg7p9f766IQspC5oqaqZVX2c=" crossorigin="anonymous"></script>
<script>
function orderByChanged() {
$('form').submit();
}
</script>
}
控制器:
请务必在首次渲染视图时设置 ViewBag.CategoryId
和 ViewBag.SearchName
。
public IActionResult Products(int? page, int? categoryId, string searchName = "", string sortOrder = "")
{
int pageSize = 1;
int pageNumber = (page ?? 1);
ViewBag.SearchName = searchName;
ViewBag.CategoryId = categoryId;
ViewBag.SortOrder = sortOrder;
//....
}
public async Task<IActionResult> Index()
{
ViewBag.CategoryId = 1;
ViewBag.SearchName = "aaa";
return View();
}
结果: