英文:
How to persist model filter values on a Razor page after post
问题
我有一个相当基本的.NET Core Razor页面,其中包含一个带有表格的部分视图。主页有一组文本框和下拉菜单,用于过滤表格。表格和过滤功能按预期工作。
我还有一个模态弹出窗口,用于向基本表格添加新记录。模态上有一个提交按钮,用于保存表单值。这也按预期工作,但在提交后,用于过滤的文本框和下拉菜单会重置。
CSHTML过滤
<form>
<select style="width:150px;" asp-for="Division" asp-items="Model.Divisions">
<option value="">All</option>
</select>
<input type="submit" value="Filter" />
<a class="link-button">Reset</a>
</form>
CSHTML C#
public class IndexModel : PageModel
{
public SelectList? Divisions { get; set; }
public SelectList? OpCos { get; set; }
public SelectList? Municipalities { get; set; }
public SelectList? DesignPhases { get; set; }
public SelectList? Designers { get; set; }
[BindProperty(SupportsGet = true)]
public string? Division { get; set; }
public async Task OnGetAsync()
{
IQueryable<string> filterQuery =
from m in _context.GasProjects
where !string.IsNullOrEmpty(m.Division)
orderby m.Division
select m.Division;
Divisions = new SelectList(await filterQuery.Distinct().ToListAsync(), Division);
GasProjects = await QueryProjects();
}
}
模态后台处理程序
public async Task<IActionResult> OnPostCreateNewRecordAsync(GasProject gasProject)
{
if (!ModelState.IsValid)
{
return Page();
}
//save record
return RedirectToPage("Index");
}
我尝试过一些ViewState和JavaScript的想法,但还没有找到正确的解决方案。
英文:
I have a pretty basic .NET Core Razor page that contains a partial view with a table. The index page has a collection of text boxes and dropdowns that are used to filter the table. The table and the filtering functionality works as expected.
I also have a modal popup that is used to add a new record to the base table. There is a submit button on the modal for saving the form values. This also works as expected, except that the text boxes and dropdowns for the filtering are reset after postback.
CSHTML filter
<form>
<select style="width:150px;" asp-for="Division" asp-items="Model.Divisions">
<option value="">All</option>
</select>
<input type="submit" value="Filter" />
<a class="link-button">Reset</a>
</form>
CSHTML c#
public class IndexModel : PageModel
{
public SelectList? Divisions { get; set; }
public SelectList? OpCos { get; set; }
public SelectList? Municipalities { get; set; }
public SelectList? DesignPhases { get; set; }
public SelectList? Designers { get; set; }
[BindProperty(SupportsGet = true)]
public string? Division { get; set; }
public async Task OnGetAsync()
{
IQueryable<string> filterQuery =
from m in _context.GasProjects
where !string.IsNullOrEmpty(m.Division)
orderby m.Division
select m.Division;
Divisions = new SelectList(await filterQuery.Distinct().ToListAsync(), Division);
GasProjects = await QueryProjects();
}
Modal Post Handler
public async Task<IActionResult> OnPostCreateNewRecordAsync(GasProject gasProject)
{
if (!ModelState.IsValid)
{
return Page();
}
//save record
return RedirectToPage("Index");
}
I have tried some ViewState and javascript ideas but I haven't hit on the correct solution yet.
答案1
得分: 0
你应该使用<input type="hidden" ...>
来保存Division
的值。并使用TempData来实现它。
<form method="get">
<select style="width:150px;" asp-for="Division" asp-items="Model.Divisions">
<option value="">All</option>
</select>
<input type="submit" value="Filter" />
<a href="/YourController/Reset" class="link-button">Reset</a>
@if (!string.IsNullOrEmpty(Model.Division))
{
<input type="hidden" asp-for="Division" value="@Model.Division" />
}
</form>
你可以在OnGetAsync
方法中保存SelectedDivision数据,并在OnPostCreateNewRecordAsync
方法中检查TempData["SelectedDivision"]
的值。
public async Task OnGetAsync()
{
if (!string.IsNullOrEmpty(Request.Query["Division"]))
{
Division = Request.Query["Division"].ToString();
TempData["SelectedDivision"] = Division;
}
else if (TempData.ContainsKey("SelectedDivision"))
{
Division = TempData["SelectedDivision"].ToString();
}
// ...
IQueryable<string> filterQuery = ...;
Divisions = new SelectList(await filterQuery.Distinct().ToListAsync(), Division);
GasProjects = await QueryProjects();
}
public async Task<IActionResult> OnPostCreateNewRecordAsync(GasProject gasProject)
{
if (!ModelState.IsValid)
{
return Page();
}
// 保存记录
if (TempData.ContainsKey("SelectedDivision"))
{
Division = TempData["SelectedDivision"].ToString();
}
return RedirectToPage("Index");
}
英文:
You should use <input type="hidden" ...>
to save the value of Division
. And use TempData to implement it.
<form method="get">
<select style="width:150px;" asp-for="Division" asp-items="Model.Divisions">
<option value="">All</option>
</select>
<input type="submit" value="Filter" />
<a href="/YourController/Reset" class="link-button">Reset</a>
@if (!string.IsNullOrEmpty(Model.Division))
{
<input type="hidden" asp-for="Division" value="@Model.Division" />
}
</form>
You can save SelectedDivision data in OnGetAsync
method, and check the value of TempData["SelectedDivision"]
in the OnPostCreateNewRecordAsync method or not.
Like below:
public async Task OnGetAsync()
{
if (!string.IsNullOrEmpty(Request.Query["Division"]))
{
Division = Request.Query["Division"].ToString();
TempData["SelectedDivision"] = Division;
}
else if (TempData.ContainsKey("SelectedDivision"))
{
Division = TempData["SelectedDivision"].ToString();
}
// ...
IQueryable<string> filterQuery = ...;
Divisions = new SelectList(await filterQuery.Distinct().ToListAsync(), Division);
GasProjects = await QueryProjects();
}
And
public async Task<IActionResult> OnPostCreateNewRecordAsync(GasProject gasProject)
{
if (!ModelState.IsValid)
{
return Page();
}
// Save record
if (TempData.ContainsKey("SelectedDivision"))
{
Division = TempData["SelectedDivision"].ToString();
}
return RedirectToPage("Index");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论