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>
<%} %>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With