代码之家  ›  专栏  ›  技术社区  ›  mare

在ASP.NET MVC中使用EditCreate.ascx partial编辑和创建视图

  •  1
  • mare  · 技术社区  · 15 年前

    如果你看一下NerdDinner创建和编辑晚餐的例子,你会发现他们使用一个partial(ViewUserControl或ASCX)dinneform将创建和编辑晚餐的功能放在一个文件中,因为它是相同的,并且他们使用RenderPartial(“dinneform”)。

    这种方法对我来说似乎很好,但我遇到了一个问题,您必须向表单标记添加额外的路由值或html属性。

    <% using (Html.BeginForm()) { %>
    

    但是,如果我使用另一个BeginForm()重载,允许传入enctype或任何其他属性,我必须这样做:

    <% using ("Create", "Section", new { modal = true }, FormMethod.Post, new { enctype = "multipart/form-data" }))
    

    在这种情况下我该怎么办?

    2 回复  |  直到 15 年前
        1
  •  3
  •   anthonyv    15 年前

        public static MvcHtmlString CurrentAction(this HtmlHelper htmlHelper)
        {
            return htmlHelper.ViewContext.RouteData.Values["action"];
        }
    
        public static MvcHtmlString CurrentController(this HtmlHelper htmlHelper)
        {
            return htmlHelper.ViewContext.RouteData.Values["controller"];
        }
    

    然后你可以这样做:

    <% using (Html.CurrentAction, Html.CurrentController, new { modal = true }, FormMethod.Post, new { enctype = "multipart/form-data" }))
    

    public static MvcForm BeginForm(this HtmlHelper htmlHelper, object routeValues, FormMethod method, object htmlAttributes)
    {
        return htmlHelper.BeginForm(Html.CurrentAction, Html.CurrentController, routeValues, method, htmlAttributes);
    }
    

    如果这有帮助,请告诉我。 干杯

        2
  •  0
  •   Community CDub    8 年前

    我在回答一个旧的线程,因为它出现了第二个在谷歌搜索,而我正在寻找相同的东西:)发现在这个SO的帖子:

    Html.BeginForm and adding properties

    使用MVC3(不确定MVC2),可以为操作和控制器传入null,并获取BeginForm()将使用的默认路由。

    @Html.BeginForm(null, null, FormMethod.Post, new { enctype="multipart/form-data"})
    

    干杯!