简体   繁体   English

具有不同模型的ASP.NET MVC部分视图

[英]ASP.NET MVC Partial View with different Model

In my Index view I have the usual Edit, Details and Delete links. 在“索引”视图中,我具有通常的“编辑”,“详细信息”和“删除”链接。 I've made icons for them, and since I use them everywhere I placed them in a Partial View. 我已经为它们制作了图标,并且由于在各处都使用了它们,因此将它们放置在“部分视图”中。

Now, I do not have the feeling I'm doing it best practice here. 现在,我没有在这里做最佳实践的感觉。 So my question is: How to optimize it, or what should be different for best practice. 所以我的问题是:如何优化它,或者最佳实践应该有什么不同。

The Partial View: 部分视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MVC2_NASTEST.Models.ButtonDetail>" %>
<%if (Model.edit) { %>
<a href='<%= Url.Action("Edit", Model.RouteValues) %>'>
    <img src="<%= Url.Content("~/img/pencil.png") %>" alt="Edit" width="16" /></a>
<%} if (Model.details) { %>
<a href='<%= Url.Action("Details",Model.RouteValues) %>'>
    <img src="<%= Url.Content("~/img/application_view_detail.png") %>" alt="Details" width="16" /></a>
<%} if (Model.delete) { %>
<a class="delbtn" href='<%= Url.Action("Delete",Model.RouteValues) %>'>
    <img src="<%= Url.Content("~/img/cancel.png") %>" alt="Delete" width="16" /></a>
<%} %>

The ViewModel: ViewModel:

using System;
namespace MVC2_NASTEST.Models {

    public partial class ButtonDetail {
        public object RouteValues { get; set; }
        public bool edit { get; set; }
        public bool details { get; set; }
        public bool delete { get; set; }
    }
}

can or should viewmodels be in the Models namespace? viewmodels可以还是应该在Models名称空间中? or place them in a different namespace? 或将它们放置在其他名称空间中? what is best practice here? 什么是最佳做法?

The Index View: 索引视图:

    <% foreach (var item in Model) { %>
    <tr>
        <td>
            <% Html.RenderPartial("buttons", new MVC2_NASTEST.Models.ButtonDetail() { RouteValues = new { id = item.hlpb_ID, test = item.hlpb_Ontvanger }, edit = true, details = true, delete = true }); %>
        </td>
        <td>
            <%: item.hlpb_Titel %>
        </td>
        <td>
            <%: item.hlpb_Schooljaar %>
        </td>
        <td>
           ...
        </td>
    </tr>
    <% } %>

Especially the part in the Index view doesn't look best practice in my eyes. 特别是在索引视图中,该部分在我眼中看起来不是最佳实践。

can or should viewmodels be in the Models namespace? viewmodels可以还是应该在Models名称空间中?

To separate them from the Models you could place them in the ViewModels namespace. 要将它们与模型分开,可以将它们放在ViewModels名称空间中。 As far as the Index view is concerned you could use Editor/Display templates . 就索引视图而言,您可以使用编辑器/显示模板

I think the partial does not help you here. 我认为部分内容对您没有帮助。

<a href='<%= Url.Action("Edit") %>'>
    <img src="<%= Url.Content("~/img/pencil.png") %>" alt="Edit" width="16" /></a>

Is not that worse then 那还不那么糟

<% Html.RenderPartial("buttonEdit"); %>

Even if you find that all buttons should share the same style I would just use css here. 即使您发现所有按钮应该共享相同的样式,我也将在此处使用css。

As a side note don't use a get for the delete action. 附带说明一下,请勿将get用于删除操作。 A search crawler might delete all your entries while scanning your site. 搜索搜寻器可能会在扫描您的网站时删除所有条目。 Even if its an internal site it is not a good idea. 即使是内部站点,也不是一个好主意。 Use post. 使用帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM