首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MVC2中重构表单

在MVC2中重构表单
EN

Stack Overflow用户
提问于 2010-09-07 22:08:01
回答 2查看 571关注 0票数 2

我发现自己一遍又一遍地在处理表单的视图中粘贴这些代码。有没有一种简单的方法可以从MVC2中的视图重构以下标记?唯一变化的部分是cancel链接的路由(LocalizedSaveButton和LocalizedCancelLink是我创建的助手方法)。我尝试将其解压缩到PartialView,但失去了BeginForm功能。有什么建议吗?

代码语言:javascript
复制
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div class="span-14">
    <% Html.EnableClientValidation(); %>
    <%using (Html.BeginForm())
      {%>
    <fieldset>
        <legend>Edit Profile</legend>
        <%=Html.AntiForgeryToken() %>
        <%=Html.EditorForModel() %>
        <div class="span-6 prepend-3">
            <%=Html.LocalizedSaveButton() %>
            <%=Html.LocalizedCancelLink(RoutingHelper.HomeDefault()) %>                
        </div>
    </fieldset>
    <%}%>
</div>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-07 22:16:11

您可以将代码包装在BeginForm这样的超文本标记语言扩展中。从您的代码中,在正确的位置调用BeginForm。

您应该返回一个实现IDisposable的对象。在dispose中,将存储结果的Dispose调用到BeginForm。

你最终会得到:

代码语言:javascript
复制
<% using (Html.MyBeginForm()) { %>
    <%=Html.LocalizedSaveButton() %> 
    <%=Html.LocalizedCancelLink(RoutingHelper.HomeDefault()) %>   
<% } %>

诀窍不是返回字符串或MvcHtmlString,而是直接使用以下命令编写输出:

代码语言:javascript
复制
htmlHelper.ViewContext.Writer.Write(....);

它会做类似这样的事情:

代码语言:javascript
复制
public class MyForm : IDisposable {
    private MvcForm _form;
    private ViewContext _ctx; 

    public MyForm(HtmlHelper html, /* other params */) {
       _form = html.BeginForm();
       _ctx = html.ViewContext;
    }

    public Dispose() {
        _form.Dispose();
        _ctx.Writer.Write("html part 3 => closing tags");
    }
}

和扩展名:

代码语言:javascript
复制
public static MyForm MyBeginForm(this HtmlHelper html /* other params */) {
    html.ViewContext.Writer.Write("html part 1");
    var result = new MyForm(html);
    html.ViewContext.Writer.Write("html part 2");
    return result;
}

免责声明:这是未经测试的代码。

票数 1
EN

Stack Overflow用户

发布于 2010-09-08 02:22:10

这是我想出来的:它基本上可以工作,但是我还不能让Html.EnableClientValidation()与呈现的形式协同工作。

代码语言:javascript
复制
namespace System.Web.Mvc
{
        public class MyForm : IDisposable
        {
            private bool _disposed;
            private readonly HttpResponseBase _httpResponse;

        public MyForm(HttpResponseBase httpResponse)
        {
            if (httpResponse == null)
            {
                throw new ArgumentNullException("httpResponse");
            }
            _httpResponse = httpResponse;
        }

        public void Dispose()
        {
            Dispose(true /* disposing */);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                _disposed = true;
                _httpResponse.Write("</form>");
            }
        }

        public void EndForm()
        {
            Dispose(true);
        }
    }
}

public static class MyFormExtensions
{
    public static MyForm FormHelper(
        this HtmlHelper htmlHelper, 
        string formAction, 
        FormMethod method, 
        IDictionary<string, object> htmlAttributes, 
        string formLegendTitle)
    {
        TagBuilder tagBuilder = new TagBuilder("form");

        tagBuilder.MergeAttributes(htmlAttributes);

        tagBuilder.MergeAttribute("action", formAction);
        tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
        HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
        httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag));

        return new MyForm(httpResponse);
    }



    public static MyForm MyBeginForm(this HtmlHelper html, string formLegendTitle) {

        string formAction = html.ViewContext.HttpContext.Request.RawUrl;
        var result = FormHelper(html,formAction, FormMethod.Post, null, formLegendTitle );

            html.ViewContext.Writer.Write("<fieldset>");
            html.ViewContext.Writer.Write(String.Format("<legend>{0}</legend>", formLegendTitle));
            html.ViewContext.Writer.Write("<div class=\"span-14\">");
            html.ViewContext.Writer.Write(html.AntiForgeryToken());
            html.ViewContext.Writer.Write(html.EditorForModel());
            html.ViewContext.Writer.Write("<div class=\"span-6 prepend-3 buttons\">");
            html.ViewContext.Writer.Write(html.LocalizedSaveButton());
            html.ViewContext.Writer.Write("</div>");
            html.ViewContext.Writer.Write("</div>");
            html.ViewContext.Writer.Write("</fieldset>");

        return result;

    }

    public static void EndForm(this HtmlHelper htmlHelper)
    {
        HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
        httpResponse.Write("</form>");
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3659391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档