如何干净地重用Asp.NET MVC编辑/新观点 [英] How to cleanly reuse edit / new views in Asp.NET MVC

查看:110
本文介绍了如何干净地重用Asp.NET MVC编辑/新观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图避免code这样重复使用相同的ViewUserControl在ASP.NET MVC的时候。有什么建议?

 <若%(ViewContext.ViewData 的ToString()==编辑){%>
    &其中;%= Html.SubmitButton(提交,更新牌)%>&所述;跨度类=或>或&所述; /跨度>&所述; A HREF =#类=取消>取消< / A>
<%)其他(%>
    <%= Html.SubmitButton(提交,创建新品牌)%><跨度类=或>或< / SPAN>< A HREF =#级=取消&GT ;取消< / A>
<%}%GT;
 

 <若%(ViewContext.ViewData 的ToString()==编辑){%>
    < H1类=编辑>编辑品牌详情< / H1>
<%)其他(%>
    < H1类=创造>创建一个新的品牌和LT; / H1>
<%}%GT;
 

解决方案

我一直都创造了新的和编辑不同的意见,否则那感觉就像我的应用程序的逻辑已经开始蔓延到我的看法。同样,我对创建和更新不同的控制器动作。也许是一个更好的方式来处理,这将是走位,这两个观点的份额,并将其移动到用户的控制和做的RenderPartial。这样,你可以有一个单一的模式,干净的意见,但只写共用部位一次。

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.

这篇关于如何干净地重用Asp.NET MVC编辑/新观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆