多个视图与一个“复杂”视图的比较在 MVC 中查看

发布于 2024-10-05 00:45:54 字数 627 浏览 3 评论 0原文

这是在我们的一次回顾中出现的,需要一些额外的反馈和抽查。目前,我们有许多基于布尔标志启用/禁用的视图(Model.IsNew 是一个示例)。我认为视图应该尽可能简单,控制器应该确定该视图的数据,而不一定是它的工作方式。我认为视图,无论是部分的还是完整的,都应该被告知要做什么和处理它,而不是由视图决定应该显示/隐藏什么。一个非常基本的示例如下,但涵盖了这两个方面,并且主要反映了我们所拥有的内容......

控制器有一对称为“Details”的方法(post/get)。 [Get]Details 有一个参数,Id,[Post]Details 接受 id 和一个 viewmodel。在帖子中,该方法大约 30 行长,检查有效模型,确定其是否是新的,某个值是否更改(触发重定向)等等(我认为这是不正确的)。 [Get]Details 检查空 ID,填充必要的下拉列表,没什么花哨的(我认为这是正确的)。详细视图本身包含一点逻辑: If (!Model.IsNew) { RenderAction(History => History.Show(id); } (我认为 if 中的这个是不正确的,Show 应该知道要显示什么,无论它是否是新的)。优点是所述详细信息视图的布局不会重复两次,减去一些禁用的字段,具体取决于状态(也许这些应该是部分的?) -实体可以被禁用/删除,从而使值可编辑或不可编辑吗

This came up during one of our retrospectives and wanted some additional feedback and a spot check. We currently have a number of views that enable/disable based on boolean flags (Model.IsNew is an example). I'm of the opinion that views should be as simple as possible and controllers should determine the data for that view, not necessarily how it works. I think views, partial or full, should be -told- what to do and handle that vs the view determining what should be shown/hidden. A very basic example goes as follows but covers both sides of this and is mostly a reflection of what we have ...

A controller has a pair of methods (post/get) called Details. [Get]Details has a single parameter, Id and [Post]Details takes id and a viewmodel. Within the post, the method is ~30 lines long checking for valid model, determining if its new, if a certain value changed (triggers redirect) and so on (I think this is incorrect). The [Get]Details check for empty id, populates the necessary dropdowns, nothing fancy (I think this is right). The detail view itself contains a small bit of logic : If (!Model.IsNew) { RenderAction(History => History.Show(id); } (I think this within an if is incorrect, the Show should know what to display, regardless if its new). The plus to this is the layout for said Detail view isn't done twice. Details/Add would be nearly identical, minus some disabled fields depending on state (maybe these should be partials?) -- the entity could be disabled/deleted making the values editable or not.

Thoughts, opinions, insights?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅一夜美梦 2024-10-12 00:45:54

为什么不创建多个视图和操作?

Details

Edit
[HttpPost]
Edit

Create
[HttpPost]
Create

然后他们可以共享一个模型对象

public ThingModel
{
    public Thing Thing { get; set; }
}

,或者让创建和编辑操作使用更安全的(防止 html 注入)模型,这也将允许您使用内置的验证选项。

public ThingEditorModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
    public bool IsNew { get { return Id == 0; } }
}

然后对于编辑和创建,您可以创建一个 EditorTemplate(共享/EditorTemplates) /ThingEditor.ascx),创建和编辑可以共享

why not create multiple Views and Actions?

Details

Edit
[HttpPost]
Edit

Create
[HttpPost]
Create

Then they could share a Model object

public ThingModel
{
    public Thing Thing { get; set; }
}

or have the Create and Edit actions use a safer (prevent html injection) model which will also let you use the Validation options built in.

public ThingEditorModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
    public bool IsNew { get { return Id == 0; } }
}

And then for Edit and Create you can create an EditorTemplate (Shared/EditorTemplates/ThingEditor.ascx) that the Create and Edit could share

南风几经秋 2024-10-12 00:45:54

我认为您走在正确的轨道上......使用“主”视图进行布局,然后使用模板化助手进行“逻辑”。依靠 Html.DisplayFor(x=>x.Thing) 和 EditorFor

你肯定不希望布局在两个地方。

I think you're on the right track...use the 'main' view for layout and then use Templated Helpers for the 'logic'. Lean on Html.DisplayFor(x=>x.Thing) and EditorFor

You definately don't want the layout in two places.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文