<p>The Add view and the Edit view are often incredibly similar that it is unwarranted to write 2 views. As the app evolves you would be making the same changes to both.</p>

<p>However, there are usually subtle differences. For instance, a field might be read-only once it's been added, and if that field is a DropDownList you no longer need that List in the ViewData.</p>

<p>So, should I create a view data class which contains all the information for both views, where, depending on the operation you're performing, certain properties will be null?<br>
Should I include the operation in the view data as an enum?<br>
Should I surround all the subtle differences with <em>&lt;% if( ViewData.Model.Op == Ops.Editing ) { %></em> ?</p>

<p>Or is there a better way?</p>

[{'answer_id': 18876, 'author': 'Andrew Rimmer', 'author_id': 1982, 'author_profile': 'https://Stackoverflow.com/users/1982', 'pm_score': 2, 'selected': False, 'text': "<p>I don't like the Views to become too complex, and so far I have tended to have separate views for Edit and Add. I use a user control to store the common elements to avoid repetition. Both of the views will be centered around the same ViewData, and I have a marker on my data to say whether the object is new or an existing object. </p>\n\n<p>This isn't any more elegant than what you have stipulated, so I wonder if any of the Django or Rails guys can provide any input. </p>\n\n<p>I love asp.net mvc but it is still maturing, and still needs more sugar adding to take away some of the friction of creating websites.</p>\n"}, {'answer_id': 18956, 'author': 'Jim', 'author_id': 1208, 'author_profile': 'https://Stackoverflow.com/users/1208', 'pm_score': 2, 'selected': False, 'text': '<p>I personally just prefer to use the if/else right there in the view.  It helps me see everything going on in view at once.  </p>\n\n<p>If you want to avoid the tag soup though, I would suggest creating a helper method.  </p>\n\n<pre><code>&lt;%= Helper.ProfessionField() %&gt;\n\nstring ProfessionField()\n{\n    if(IsNewItem) { return /* some drop down code */ }\n    else { return "&lt;p&gt;" + _profession+ "&lt;/p&gt;"; }        \n}\n</code></pre>\n'}, {'answer_id': 41866, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 3, 'selected': False, 'text': '<p>It\'s pretty easy really.  Let\'s assume you\'re editing a blog post.</p>\n\n<p>Here\'s your 2 actions for new/edit:</p>\n\n<pre><code>public class BlogController : Controller\n{\n   public ActionResult New()\n   {\n      var post = new Post();\n      return View("Edit", post);\n   }\n\n   public ActionResult Edit(int id)\n   {\n      var post = _repository.Get(id);\n      return View(post);\n   }\n\n   ....\n\n}\n</code></pre>\n\n<p>And here\'s the view:</p>\n\n<pre><code>&lt;% using(Html.Form("save")) { %&gt;\n&lt;%= Html.Hidden("Id") %&gt;\n\n&lt;label for="Title"&gt;Title&lt;/label&gt;\n&lt;%= Html.TextBox("Title") %&gt;\n\n&lt;label for="Body"&gt;Body&lt;/label&gt;\n&lt;%= Html.TextArea("Body") %&gt;\n\n&lt;%= Html.Submit("Submit") %&gt;\n&lt;% } %&gt;\n</code></pre>\n\n<p>And here\'s the Save action that the view submits to:</p>\n\n<pre><code>public ActionResult Save(int id, string title, string body)\n{\n   var post = id == 0 ? new Post() : _repository.Get(id);\n   post.Title = title;\n   post.Body = body;\n\n   _repository.Save(post);\n\n   return RedirectToAction("list");\n}\n</code></pre>\n'}, {'answer_id': 290841, 'author': 'labilbe', 'author_id': 1195872, 'author_profile': 'https://Stackoverflow.com/users/1195872', 'pm_score': 1, 'selected': False, 'text': "<p>You can specify a CustomViewData class and pass the parameters here.</p>\n\n<pre><code>\npublic class MyViewData {\n    public bool IsReadOnly { get; set; }\n    public ModelObject MyObject { get; set; }\n}\n</code></pre>\n\n<p>And both views should implement this ViewData.\nAs a result you can use provided IsReadOnly property to manage the UserControl result.</p>\n\n<p>As the controller uses this, you can unit test it and your views doesn't have implementation, so you can respect the MVC principles.</p>\n"}]
['https://Stackoverflow.com/questions/18757', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1851/']