【问题标题】:Partial view - avoiding nested forms部分视图 - 避免嵌套表单
【发布时间】:2013-10-21 14:44:20
【问题描述】:

查看

@using (Html.BeginForm())
{
    ...form elements
    @Html.Action("PartialView")
}

部分视图

if (something) {
    <input type="submit" value="Submit" />
} else {
    @using (Html.BeginForm())
    {
        <input type="submit" value="Submit" />
    }

有人可以提出解决上述问题的方法吗?

如果 PartialView if 语句返回 false,我将得到嵌套表单。我可以在局部视图中移动表单右括号以避免嵌套表单并且页面正确呈现,但这会使 Visual Studio 感到不安,因为它希望在视图中看到右括号。这有关系吗?

编辑:

基于 Chris 的 cmets,以下修改是更好的方法吗?即一个表单有两个提交按钮,在同一个操作方法中调用不同的代码?

部分视图

if (something) {
    <input type="submit" name="btn" value="Submit1" />
} else {
    <input type="submit" name="btn" value="Submit2" />
}

控制器

[HttpPost]
public ActionResult Index()
{
    if (btn == "Submit1") {
        ...do a thing
    } else {
        ...do another thing
    };
}

【问题讨论】:

  • dont use &lt;form&gt; inside another &lt;form&gt;
  • 谢谢穆拉利。那就是问题所在。解决办法是什么?
  • 你能不能设计局部视图,使其不必拥有自己的Html.BeginForm
  • 也许我必须这样做,克里斯。目前,每个表单调用不同的操作方法。也许他们都应该调用相同的操作方法,其中包含一个 if 语句,根据调用的形式返回 true 或 false。
  • 我会说这将是一个更好的方法。在 Web 表单中,子表单总是一个问题,因为整个页面都是一个表单。因此,为每个“表单”简单地命名提交按钮是非常典型的。它们并不是真正独立的表单,但是通过给提交按钮一个唯一的名称,您可以检查它是否存在于服务器端的发布数据中,并确定用户发布了哪个“表单”。

标签: asp.net-mvc razor partial-views


【解决方案1】:

另一个&lt;form&gt; 中的&lt;form&gt; 标记不是有效的HTML

参考W3c Spec

可用的解决方法

http://blog.avirtualhome.com/how-to-create-nested-forms/

【讨论】:

  • 我不确定你是否理解这个问题。
【解决方案2】:

我遇到了同样的问题,并想出了一个真正解决它的助手。

/**
 * Ensure consequent calls to Html.BeginForm are ignored. This is particularly useful
 * on reusable nested components where both a parent and a child begin a form.
 * When nested, the child form shouldn't be printed.
 */
public static class SingleFormExtensions
{
    public static IDisposable BeginSingleForm(this HtmlHelper html)
    {
        return new SingleForm(html);
    }

    public class SingleForm: IDisposable
    {
        // The form, if it was needed
        private MvcForm _form;

        public SingleForm(HtmlHelper html)
        {
            // single per http request
            if (!HttpContext.Current.Items.Contains(typeof(SingleForm).FullName))
            {
                _form = html.BeginForm();
                HttpContext.Current.Items[typeof(SingleForm).FullName] = true; // we need the key added, value is a dummy
            }
        }

        public void Dispose()
        {
            // close the form if it was opened
            if (_form != null)
            {
                _form.EndForm();
                HttpContext.Current.Items.Remove(typeof(SingleForm).FullName);
            }
        }
    }
}

要使用它,请包含扩展名的命名空间并在任何你想要的地方执行@Html.BeginSingleForm(。不仅在嵌套视图中,而且在父视图中。

兴趣点:需要保存表单是否较早打开。我们不能有一个静态或静态的每个线程变量ThreadStatic,因为这可能被许多 Asp.Net 线程使用。添加变量的唯一单线程和按 http 请求的位置是 HttpContext.Current.Items 字典。

提交按钮的数量没有限制。问题是嵌套的form 元素。为避免有多个提交按钮,您可以使用 jquery 隐藏它们,或者扩展此帮助程序以在末尾自动添加提交按钮。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2016-11-19
    • 2014-08-23
    • 2018-10-07
    相关资源
    最近更新 更多