gpt4 book ai didi

forms - 使用参数提交表单到操作,Asp.Net.Core Mvc

转载 作者:行者123 更新时间:2023-12-04 15:27:52 25 4
gpt4 key购买 nike

我正在尝试通过 asp.net.core 中的 ajax 提交排序下拉表单。 Action 还有其他参数,我也想在 Action 中使用这些参数,但表单不提交其他参数。我认为,主要问题是 Razor 的形式部分

这是我行动的一部分:

       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>

最佳答案

这是一个像下面这样的工作演示:

View (索引.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>
}

Controller :

请务必在首次呈现 View 时设置 ViewBag.CategoryIdViewBag.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();
}

结果: enter image description here

关于forms - 使用参数提交表单到操作,Asp.Net.Core Mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61900617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com