发布部分视图表格 [英] Post partial view form

查看:81
本文介绍了发布部分视图表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我理解ASP.NET Core Razor中的部分视图,表单和发布.

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

我在〜/Client/Search"中有一个Search.cshtml部分视图:

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

带有代码页Search.cshtml.cs:

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

如果我直接加载〜/Client/Search"局部视图,它将在加载后正确触发OnPosAsync()操作.

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

但是,如果〜/Client/Search"局部视图是从〜/Session/CheckIn"父视图呈现的:

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

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

〜/Client/Search"部分视图中的OnPostAsync()不再触发.

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

我已经尝试了各种组合来在Partial View的Html.BeginForm中定义动作",控制器",但是我永远无法触发Partial View的OnPostAsync().

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.

有指针吗?阅读了很多文章和论坛帖子,但是并没有清晰的描述或演练来帮助我理解这一点,并使Partial View操作方法在从父视图发回时触发.

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.

推荐答案

这就是为什么Razor Pages令人讨厌的原因:它们混淆了逻辑,使人们无需真正了解任何工作原理就可以构建东西.</rant>

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>

局部视图没有什么特别的.这只是一个与其他任何视图相同的视图.使它部分"的是使用它的上下文,即将其注入视图的呈现中.这样,Razor Pages允许您在代码后添加代码,因为它只是一个视图,任何视图都可以在代码后置Razor Pages.但是,当像部分代码一样使用时,实际上并没有利用后面的代码,这就是您的问题.

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.

此外,您需要记住,局部视图的整个概念仅存在于服务器端.返回响应后,您所拥有的只是一个HTML文档.无论您使用一个局部视图,100个局部视图还是不使用局部视图来创建响应服务器端,浏览器都不会在乎.因此,使用部分内容不会以某种方式神奇地使您购买仅处理页面的单个部分的功能,例如,在您发布时,仅更改了该部分.为此,您需要AJAX.否则,无论是否从局部视图"进行发布,都会导致整个视图在浏览器窗口中被更改.

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.

换句话说,您需要服务器端通过返回一个新的完整视图(不仅仅是部分视图)来响应该发布请求的东西,或者您需要通过AJAX发出请求客户端,并仅返回您的部分视图看法.但是,那么,您有责任自己亲自替换所有应替换为该响应的HTML.

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.

这篇关于发布部分视图表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆