ASP.NET MVC 5 – 一个视图用于多个操作方法?

huangapple go评论119阅读模式
英文:

ASP.NET MVC 5 - One View for multiple action methods?

问题

在我的应用程序中,我需要三个视图,显示相同的数据:员工详细信息以及员工职位(工作历史)。它们仅在一些按钮方面有所不同。

正如您在下面的图片中所看到的,详细视图显示数据,唯一可用的操作是“返回”。第二个是编辑视图。在这里,编辑仅意味着添加新的工作职位。其他员工数据不可编辑。
第三个视图用于更改员工状态(启用、禁用、删除):底部的按钮根据当前员工状态可见/隐藏。

详细信息

编辑

更改状态

您认为将所有三种方法(详细信息、编辑、更改状态)合并为一个视图是一个好的选择吗?

传递给视图的ViewModel应该具有属性,以便评估以显示/隐藏按钮。
例如:当前状态(启用、禁用、已删除)、操作(显示详细信息、编辑、更改状态)。

我还考虑使用两个部分视图来显示员工详细信息和工作职位部分。

英文:

In my application I need three views that shows the same data :Employee details data toghether with employee job positions (job history). They differ only for some buttons.

AS you can see in the images below the Details view shows data and the only available action is "go back". The second is the EditView. Editing, here, means only add a new job position. Other employee data are not editable.
The third view is for changing the employee state (Enabled, Disabled, Deleted) : buttons in the footer are visible\hidden based on current employee state.

Details

ASP.NET MVC 5 – 一个视图用于多个操作方法?

Edit

ASP.NET MVC 5 – 一个视图用于多个操作方法?

ChangeState

ASP.NET MVC 5 – 一个视图用于多个操作方法?

Do you think is a good option to have one view for all three methods (Detail, Edit, ChangeState) ?

The ViewModel passed to the view should have properties to be evaluated in order to show\hide buttons.
For example : CurrentState (enabled, disabled, deleted), Action (ShowDetail, Edit, ChangeState).

I was also thinking to use two partial views for employee details and job position sections.

答案1

得分: 1

你考虑过使用部分视图吗?

Controller:

[Route("no-partial")]
public IActionResult Index()
{
    var model = new CustomModel()
    {
        Name = "Home",
    };

    return View("~/Views/Home/Index.cshtml", model);
}

[Route("with-partial")]
public IActionResult IndexWithPartialView()
{
    var model = new CustomModel()
    {
        Name = "Home",
        PartialViewPath = "~/Views/Shared/_MyPartialView.cshtml"
    };

    return View("~/Views/Home/Index.cshtml", model);
}

View:

@model CustomModel

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1>Welcome to homepage</h1>
    @if (Model.PartialViewPath != null)
    {
        @await Html.PartialAsync(Model.PartialViewPath)
    }
</div>

部分视图:

<div>
    <h1>Hello from Partial View</h1>
</div>

结果:

https://localhost:7230/no-partial

<main b-bddmc6yalx="" role="main" class="pb-3">
    <div class="text-center">
        <h1>Welcome to homepage</h1>
    </div>
</main>

https://localhost:7230/with-partial

<main b-bddmc6yalx="" role="main" class="pb-3">
    <div class="text-center">
        <h1>Welcome to homepage</h1>

        <div>
            <h1>Hello from Partial View</h1>
        </div>
    </div>
</main>

有关部分视图的更多信息,请参阅以下链接:https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-7.0

在我看来,这不是最佳实践。我建议创建多个较小的部分视图,然后只需使用它们构建视图。我的意思是:

Create.cshtml:
- Partial_Form
- Partial_CreateButtons
- Partial_Form2

Edit.cshtml:
- Partial_Form
- Partial_EditButtons
- Partial_Form2
- Partial_DeleteButtons

Detail.cshtml:
- Partial_Form
- Partial_Form2
- Partial_DetailButtons

您将有3个视图,但内容将从这些部分视图中构建,您将它们“硬编码”到您的视图中。因此,每当您需要编辑内容时,您将编辑较小的组件/视图,而不是庞大的通用视图。

英文:

Have you cosidered using Partial Views?

Controller:

[Route(&quot;no-partial&quot;)]
public IActionResult Index()
{
    var model = new CustomModel()
    {
        Name = &quot;Home&quot;,
    };

    return View(&quot;~/Views/Home/Index.cshtml&quot;, model);
}

[Route(&quot;with-partial&quot;)]
public IActionResult IndexWithPartialView()
{
    var model = new CustomModel()
    {
        Name = &quot;Home&quot;,
        PartialViewPath = &quot;~/Views/Shared/_MyPartialView.cshtml&quot;
    };

    return View(&quot;~/Views/Home/Index.cshtml&quot;, model);
}

View:

@model CustomModel

@{
    ViewData[&quot;Title&quot;] = &quot;Home Page&quot;;
}

&lt;div class=&quot;text-center&quot;&gt;
    &lt;h1&gt;Welcome to homepage&lt;/h1&gt;
    @if (Model.PartialViewPath != null)
    {
        @await Html.PartialAsync(Model.PartialViewPath)
    }
&lt;/div&gt;

Partial View:

&lt;div&gt;
    &lt;h1&gt;Hello from Partial View&lt;/h1&gt;
&lt;/div&gt;

Results:

https://localhost:7230/no-partial

&lt;main b-bddmc6yalx=&quot;&quot; role=&quot;main&quot; class=&quot;pb-3&quot;&gt;
    &lt;div class=&quot;text-center&quot;&gt;
        &lt;h1&gt;Welcome to homepage&lt;/h1&gt;
    &lt;/div&gt;
&lt;/main&gt;

https://localhost:7230/with-partial

&lt;main b-bddmc6yalx=&quot;&quot; role=&quot;main&quot; class=&quot;pb-3&quot;&gt;
    &lt;div class=&quot;text-center&quot;&gt;
        &lt;h1&gt;Welcome to homepage&lt;/h1&gt;

        &lt;div&gt;
            &lt;h1&gt;Hello from Partial View&lt;/h1&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/main&gt;

For more info about Partial Views, please see the following: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-7.0

This is in my opinion not the best practice. I would recommend making multiple smaller partial views and then just building the view with them. What do I mean by that is the following:

Create.cshtml:
- Partial_Form
- Partial_CreateButtons
- Partial_Form2

Edit.cshtml:
- Partial_Form
- Partial_EditButtons
- Partial_Form2
- Partial_DeleteButtons

Detail.cshtml:
- Partial_Form
- Partial_Form2
- Partial_DetailButtons

You will have 3 views, however the content will be built from these Partial Views that you will 'hard-code' into your View. Therefore, whenever you will need to edit something, you will be editing the smaller component/view rather than the huge generic View.

huangapple
  • 本文由 发表于 2023年6月15日 21:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定