【发布时间】:2018-09-26 20:54:02
【问题描述】:
谁能帮我理解 ASP.NET Core Razor 中的部分视图、表单和发布。
我在“~/Client/Search”中有一个 Search.cshtml 部分视图:
@model Web.Pages.Client.SearchModel
@using (Html.BeginForm())
{
<div>
@Html.RadioButtonFor(x => x.searchType, (int)ApplicationCore.Interfaces.SearchType.mobile, new { Name = "SearchType" }) Mobile
@Html.RadioButtonFor(x => x.searchType, (int)ApplicationCore.Interfaces.SearchType.phone, new { Name = "SearchType" }) Phone
@Html.RadioButtonFor(x => x.searchType, (int)ApplicationCore.Interfaces.SearchType.email, new { Name = "SearchType" }) Email
</div>
@Html.TextBoxFor(x => x.searchFilter)
<input type="submit" value="Search"/>
}
带有代码页 Search.cshtml.cs :
public class SearchModel : PageModel
{
public SearchType searchType { get; set; }
public string searchFilter { get; set; }
private readonly IClientService _clientService;
private readonly Infrastructure.Data.DBContext _context;
public SearchModel(Infrastructure.Data.DBContext context, IClientService clientService)
{
_context = context;
_clientService = clientService;
searchFilter = string.Empty;
searchType = SearchType.mobile;
}
public async Task<IActionResult> OnPostAsync()
{
return RedirectToPage("./Index");
}
}
如果我直接加载“~/Client/Search”部分视图,它会加载并在发布时正确触发 OnPosAsync() 操作。
但是,如果“~/Client/Search”部分视图是从“~/Session/CheckIn”父视图呈现的:
@await Html.PartialAsync("~/Client/Search", Model._searchModel)
“~/Client/Search”部分视图中的 OnPostAsync() 不再触发。
我已经尝试了各种组合来定义部分视图中的 Html.BeginForm 中的“动作”、“控制器”,但是我永远无法触发部分视图中的 OnPostAsync()。
任何指针?阅读了很多文章和论坛帖子,但是没有明确的描述或演练来帮助我理解这一点,并在从父视图回发时触发部分视图操作方法。
【问题讨论】:
标签: c# razor asp.net-core partial-views