Home Post partial view form
Reply: 0

Post partial view form

user2388
1#
user2388 Published in August 15, 2018, 5:38 am

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.376777 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO