Post partial view form

微笑、不失礼 提交于 2020-01-16 22:35:14

问题


Can someone help me understand partial views, forms and posting in ASP.NET Core Razor.

I have a Search.cshtml partial view located in "~/Client/Search" :

@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"/>
}

With code page 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");
        }
    }

If I load the "~/Client/Search" Partial View directly it loads and on post it correctly fires the OnPosAsync() action.

However if the "~/Client/Search" Partial View is rendered from the "~/Session/CheckIn" parent View :

@await Html.PartialAsync("~/Client/Search", Model._searchModel)

The OnPostAsync() within the "~/Client/Search" Partial View no longer fires.

I have tried all sorts of combinations to define "action", "controller" within the Html.BeginForm in the Partial View, however I can never get the OnPostAsync() within the Partial View to fire.

Any pointers? Read a lot of articles and forum posts however there are no clear descriptions or walkthroughs to help me understand this and get the Partial View action method firing on postback from parent View.


回答1:


This is why Razor Pages are a blight: they obfuscate logic, allowing people to build stuff without ever actually understanding how any of it works. </rant>

There's nothing special about a partial view. It's just a view like any other view. What makes it "partial" is the context in which it's used, i.e. injecting it into the rendering of a view. As such, Razor Pages lets you add a code-behind, because it's just a view, and any view can have a Razor Pages code-behind. However, when used like a partial, that code-behind is not actually utilized, and there's your problem.

Also, you need to bear in mind that the whole concept of partial views only exists server-side. Once the response has been returned, all you have is just an HTML document. The browser couldn't care less whether you used one partial view, 100 partial views or no partial views to create the response server-side. As such, using a partial doesn't somehow magically buy you the ability to just work with a single section of your page, such that when you post, only that section is changed. For that, you need AJAX. Otherwise, doing a post, whether from a "partial view" or not, will cause the entire view to be changed in the browser window.

In other words, you need something server-side that will respond to that post request by returning a new full view, not just your partial, or you need to make the request client-side via AJAX, and return just your partial view. However, then, you're responsible for replacing whatever HTML should be replaced with that response, yourself.



来源:https://stackoverflow.com/questions/49868791/post-partial-view-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!