English 中文(简体)
MVC Rule, 意见和帮助者——具体故事
原标题:MVC Logic, Views and Helpers - A Specific Story

我和一位同事正在讨论如何在一页上形成联系。 我们认为,我们是否应当使用html助手或保持非常简单的看法逻辑?

就这一项目而言,我们正在使用Casa Monorail和NVelocity探测器。 我感谢任何考虑以下两种选择并提出意见的人。

在这一故事中,目前仅用一个页。

备选办法1 - 帮助者

助教法

 var action = snail.IsActive ? "ConfirmDeactivate" : "ConfirmActivate";
   var routeValues = new Dictionary<string, string>
                      {
                       {"action", action},
                       {"querystring", "id=" + snail.ID}
                      };
   var href = UrlHelper.For(routeValues);

   var link = new XElement("a");
   link.SetAttributeValue("href", href);
   link.SetValue(action.Substring(7));

   return link.ToString();

当时,我们认为,我们只是这样说:

<li>$Html.SnailActivationSwitchLink($item)</li>

备选案文2 - 所有观点

#if($snail.IsActive)
  <a href="$Url.For("%{action= ConfirmDeactivate , querystring= id=$snail.ID }")">Deactivate</a>
  #else
   <a href="$Url.For("%{action= ConfirmActivate , querystring= id=$snail.ID }")">Activate</a>
  #end
最佳回答

对我来说,我更喜欢选择方案。 1. 更方便地对助手和更多名人采取逻辑。

问题回答

我将直截了当地建议/使用备选办法1——不管你们正在形成多少看法。

  • Make your view Dumb. - doesn matter for a a single view or 100 views.

那时对备选办法1的支持不多,时间可以发挥陈词作用!

采用南半球等概念的主要原因之一是使申请更易于改变/更可维持。 因此:

  • What if the text in the link needs to change later? Or I want to put an image in there instead of the text? Do I really want to have to rebuild the application to remove a typo in a link?

  • 更糟糕的是,如果联系案文需要在不同页上有所不同的话? 然后,我是否为每一页加上另一个助手方法?

  • 假设这一点,我会作出这一改变。 如果我问我的标志如何改变与形象的联系,或增加一个形象的等级,以提升形象? 他非常乐意查阅档案,但是如果他看见助手的话,他就不知道超文本是从哪里来的,到哪里可以改变。 即使他找到了助手,他还是知道如何改变C#代码,以做他需要什么? 他试图做的是改变案文。 为什么必须如此困难?

  • I could alleviate some of the above issues by adding two method parameters on the helper, and passing in the text for each link, but what if the text was an image tag? I ve now got HTML that I need to escape in my helper call - that s going to look pretty messy. I still couldn t edit the link element s attributes either, should I add a parameter/parameters for that as well? Where does it stop?

从本质上讲,我可以说,我可以向大多数人展示选择2,他们理解,能够加以修改。 尼斯平原超文本,很容易遵循有条件逻辑。 备选案文1增加了我不需要的复杂性和浪费。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签