views:

121

answers:

2

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.

+2  A: 

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.

Darin Dimitrov
are you suggesting that I drop the Partial View, or just to write a template which would replace this code in the Index view. I generate my model on the fly so the code would be actually the same in that case.
Stefanvds
+1  A: 

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.

Malcolm Frexner
who sais i'm using a get? :) Deletion goes via a jQuery confirmation modal and a `$.post` so no worries there. So what are you suggesting? to make 3 separate partials? the goal of using this partial is making my code DRY. So you are saying this image should just be CSS? and forget about the partial?
Stefanvds