相关疑难解决方法(0)

ASP.NET核心MVC视图组件

在ASP.NET Core MVC(以前称为MVC 6)中,有一个名为View Components的新功能区域,它似乎是部分视图的更好替代方案.我见过以下View Component Example.但目前似乎没有更多关于其使用的信息.

我正在尝试评估它是否值得使用这种模式,如果这可以/(或打算)被用作更好的方法来帮助甜甜圈缓存.

asp.net-core-mvc asp.net-core

15
推荐指数
1
解决办法
5581
查看次数

有没有什么好的理由不在核心MVC中使用ViewComponent而不是Partial View?

我是MVC的新手并且决定从.net-core开始,所以我对核心版本与旧版本的差异没有多少了解.我确实发现了下面的问题提供了一些见解,但没有帮助我决定我是否可以基本上忽略部分观点.

为什么我们应该在部分视图上使用MVC 6功能视图组件:有什么区别?

我的问题很简单 - 如果我可以使用ViewComponent做某事,有什么理由不这样做吗?

非常感谢!

以下为上下文提供的示例.

主视图调用:

ViewComponent:

<div class="modal-body" ID="modalPersonInner">
       @await Component.InvokeAsync("CreatePerson", new Person())
</div>
Run Code Online (Sandbox Code Playgroud)

部分视图:

<div class="modal-body" ID="modalPersonInner">
    @{ await Html.RenderPartialAsync("People/CreatePartialView", new Person());}
</div>
Run Code Online (Sandbox Code Playgroud)

Javascript(personCreateForm是局部视图/视图组件中的一个表单):

 var submitPersonCreate = function(evt) {

        evt.preventDefault();
        if ($(this).valid())
        {
            $.ajax({
                type: "POST",
                url: '@Url.Action("CreatePartial", "People")',
                data: $('#personCreateForm').serialize(),
                success(data) {
                    if (data === true)
                        window.location.reload();
                    else
                        $('#modalPersonInner').html(data);
                }
            });
        }

        return false;
    }
$('#personCreateForm').submit(submitPersonCreate);
Run Code Online (Sandbox Code Playgroud)

控制器代码:

  public async Task<IActionResult> CreatePartial(
        [Bind("AddressLine1,AddressLine2,AddressLine3,AddressLine4,City,Country,Email,Forename,MobileNumber,Postcode,Region,Surname,TelephoneNumber")] Person person)
    {
        if (ModelState.IsValid)
        {
            _context.Add(person);
            await _context.SaveChangesAsync();
            return Json(true);
        } …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-core-mvc asp.net-core

15
推荐指数
1
解决办法
4615
查看次数

标签 统计

asp.net-core ×2

asp.net-core-mvc ×2

asp.net-mvc ×1

c# ×1