Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cleanly reuse edit / new views in Asp.NET MVC

I'm trying to avoid code like this when reusing the same ViewUserControl in ASP.NET MVC. Any suggestions?

<% if (ViewContext.ViewData["editMode"].ToString() == "edit"){ %>
    <%= Html.SubmitButton("submit", "Update Brand")%><span class="or">Or</span><a href="#" class="cancel">Cancel</a>
<% } else { %>
    <%= Html.SubmitButton("submit", "Create New Brand")%><span class="or">Or</span><a href="#" class="cancel">Cancel</a>
<%} %>

And ...

<% if (ViewContext.ViewData["editMode"].ToString() == "edit"){ %>
    <h1 class="edit">Edit Brand Details</h1>
<% } else { %>
    <h1 class="create">Create A New Brand</h1>
<%} %>
like image 692
Kyle West Avatar asked Nov 20 '08 05:11

Kyle West


1 Answers

I've always created separate views for New and Edit otherwise it feels like my application logic is starting to creep into my view. Similarly, I have different controller actions for Create and Update. Perhaps a better way to approach this would be to take the bits that the two view share and move them to a user control and do a RenderPartial. That way you can have clean views with a single mode but only write the common parts once.

like image 92
tvanfosson Avatar answered Oct 13 '22 02:10

tvanfosson