具有不同模型的 ASP.NET MVC 部分视图
在我的索引视图中,我有常用的编辑、详细信息和删除链接。我为它们制作了图标,由于我在任何地方都使用它们,所以我将它们放置在部分视图中。
现在,我不觉得我在这里进行了最佳实践。所以我的问题是:如何优化它,或者最佳实践应该有什么不同。
部分视图:
<%@ 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>
<%} %>
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; }
}
}
viewmodel 可以或者应该位于 Models 命名空间中吗?或者将它们放在不同的命名空间中?这里的最佳实践是什么?
索引视图:
<% 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>
<% } %>
尤其是索引视图中的部分在我看来并不是最佳实践。
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:
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? 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为部分内容对您没有帮助。
是不是更糟糕呢
即使你发现所有按钮都应该共享相同的样式,我也会在这里使用 css 。
附带说明一下,不要使用 get 来执行删除操作。搜索爬网程序可能会在扫描您的网站时删除您的所有条目。即使它是一个内部站点,这也不是一个好主意。使用帖子。
I think the partial does not help you here.
Is not that worse then
Even if you find that all buttons should share the same style I would just use css here.
As a side note don't use a get for the delete action. 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.
要将它们与模型分开,您可以将它们放置在 ViewModels 命名空间中。就索引视图而言,您可以使用 编辑器/显示模板。
To separate them from the Models you could place them in the ViewModels namespace. As far as the Index view is concerned you could use Editor/Display templates.