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

asp.net-mvcnet

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>
<%} %>

Best Answer

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.