【问题标题】:How to cleanly reuse edit / new views in Asp.NET MVC如何在 Asp.NET MVC 中干净地重用编辑/新视图
【发布时间】:2008-11-20 05:56:14
【问题描述】:

在 ASP.NET MVC 中重用相同的 ViewUserControl 时,我试图避免这样的代码。有什么建议吗?

<% 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>
<%} %>

还有……

<% if (ViewContext.ViewData["editMode"].ToString() == "edit"){ %>
    <h1 class="edit">Edit Brand Details</h1>
<% } else { %>
    <h1 class="create">Create A New Brand</h1>
<%} %>

【问题讨论】:

    标签: .net asp.net-mvc


    【解决方案1】:

    我总是为新建和编辑创建单独的视图,否则感觉就像我的应用程序逻辑开始蔓延到我的视图中。同样,我对创建和更新有不同的控制器操作。解决此问题的更好方法可能是获取两个视图共享的位并将它们移动到用户控件并执行 RenderPartial。这样你就可以在单一模式下获得干净的视图,但只编写一次公共部分。

    【讨论】:

    • 这就是我正在做的,但我仍然需要在提交按钮上设置文本。
    • 你不能通过控制器将提交按钮的文本放在你的视图数据中吗?
    • 那是把视图泄露到控制器里了,不是吗?
    【解决方案2】:

    为您的实体创建一个(或多个)部分视图(使用联系人实体的示例) - IdChange.ascx(显示 Id 和更改信息) - 个人信息.ascx - 地址.ascx

    IdChange.ascx 仅在编辑视图中需要

    创建两个单独的视图进行编辑和创建,然后使用 RenderPartial 将模型数据带到视图中。 创建.aspx

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
    <% using (Html.BeginForm())
       { %>
    <fieldset>
        <legend>Create a new contact</legend>
        <div id="pagecontent">
            <div id="left">
            </div>
            <div id="center">
                <% Html.RenderPartial("PersonalInfo", Model); %>
            </div>
        </div>
    
        <p>
            <input type="submit" value="Create" />
    

    编辑.aspx

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
    <% using (Html.BeginForm())
       { %>
    <fieldset>
        <legend>Edit existing contact</legend>
        <div id="pagecontent">
            <div id="left">
                <% Html.RenderPartial("IdChange", Model); %>
            </div>
            <div id="center">
                <% Html.RenderPartial("PersonalInfo", Model); %>
            </div>
        </div>
    
        <p>
            <input type="submit" value="Edit" />
    

    【讨论】:

      【解决方案3】:
      <% string submitLabel = (ViewData["editMode"].ToString() == "edit") ? "Update Brand" : "Create New Brand" %>
      <%= Html.SubmitButton("submit", submitLabel)%><span class="or">Or</span><a href="#" class="cancel">Cancel</a>
      

      如果您有多个这样的标签,您可以在页面顶部定义它们。

      <% 
        string submitLabel = (ViewData["editMode"].ToString() == "edit") ? "Update Brand" : "Create New Brand";
        string h1Class = (ViewData["editMode"].ToString() == "edit") ? "edit" : "create";
        string h1Label = (ViewData["editMode"].ToString() == "edit") ? "Edit Brand Details" : "Create a New Brand";
      %>
      
      
      <h1 class="<%= h1Class %>"><%= h1Label %></h1>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-30
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        相关资源
        最近更新 更多