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.
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.
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.
can or should viewmodels be in the Models namespace?
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.
精彩评论