C# 使用 ASP.NET MVC,如何最好地避免同时编写添加视图和编辑视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using ASP.NET MVC, how to best avoid writing both the Add View and Edit View?
提问by tgmdbm
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.
Add 视图和 Edit 视图通常非常相似,编写 2 个视图是没有根据的。随着应用程序的发展,您将对两者进行相同的更改。
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.
但是,通常存在细微的差异。例如,一个字段在添加后可能是只读的,如果该字段是 DropDownList,则在 ViewData 中不再需要该 List。
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?
Should I include the operation in the view data as an enum?
Should I surround all the subtle differences with <% if( ViewData.Model.Op == Ops.Editing ) { %>?
那么,我是否应该创建一个包含两个视图的所有信息的视图数据类,其中,根据您正在执行的操作,某些属性将为空?
我应该将操作作为枚举包含在视图数据中吗?
我应该用<% if( ViewData.Model.Op == Ops.Editing ) { %>包围所有细微的差异吗?
Or is there a better way?
或者,还有更好的方法?
回答by Andrew Rimmer
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.
我不喜欢视图变得太复杂,到目前为止,我倾向于对编辑和添加使用单独的视图。我使用用户控件来存储公共元素以避免重复。两个视图都以同一个 ViewData 为中心,我的数据上有一个标记来说明该对象是新对象还是现有对象。
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.
这并不比你规定的更优雅,所以我想知道是否有任何 Django 或 Rails 人可以提供任何输入。
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.
我喜欢asp.net mvc,但它仍在成熟,仍然需要添加更多的糖来消除创建网站的一些摩擦。
回答by Jim
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.
我个人更喜欢在视图中使用 if/else 。它可以帮助我立即看到正在发生的一切。
If you want to avoid the tag soup though, I would suggest creating a helper method.
如果你想避免标签汤,我建议创建一个辅助方法。
<%= Helper.ProfessionField() %>
string ProfessionField()
{
if(IsNewItem) { return /* some drop down code */ }
else { return "<p>" + _profession+ "</p>"; }
}
回答by Ben Scheirman
It's pretty easy really. Let's assume you're editing a blog post.
这真的很容易。假设您正在编辑博客文章。
Here's your 2 actions for new/edit:
这是您对新建/编辑的 2 个操作:
public class BlogController : Controller
{
public ActionResult New()
{
var post = new Post();
return View("Edit", post);
}
public ActionResult Edit(int id)
{
var post = _repository.Get(id);
return View(post);
}
....
}
And here's the view:
这是视图:
<% using(Html.Form("save")) { %>
<%= Html.Hidden("Id") %>
<label for="Title">Title</label>
<%= Html.TextBox("Title") %>
<label for="Body">Body</label>
<%= Html.TextArea("Body") %>
<%= Html.Submit("Submit") %>
<% } %>
And here's the Save action that the view submits to:
这是视图提交到的 Save 操作:
public ActionResult Save(int id, string title, string body)
{
var post = id == 0 ? new Post() : _repository.Get(id);
post.Title = title;
post.Body = body;
_repository.Save(post);
return RedirectToAction("list");
}
回答by labilbe
You can specify a CustomViewData class and pass the parameters here.
您可以指定一个 CustomViewData 类并在此处传递参数。
public class MyViewData {
public bool IsReadOnly { get; set; }
public ModelObject MyObject { get; set; }
}
And both views should implement this ViewData. As a result you can use provided IsReadOnly property to manage the UserControl result.
并且两个视图都应该实现这个 ViewData。因此,您可以使用提供的 IsReadOnly 属性来管理 UserControl 结果。
As the controller uses this, you can unit test it and your views doesn't have implementation, so you can respect the MVC principles.
当控制器使用它时,您可以对其进行单元测试并且您的视图没有实现,因此您可以尊重 MVC 原则。