单个视图中存在多个 Http.RenderAction()

发布于 2024-08-12 04:38:40 字数 598 浏览 2 评论 0原文

众所周知,RenderAction() 是

  • Asp.net MVC 1 Futures 或
  • Asp.net MVC 2 Beta 2

的一部分,允许我们在另一个视图或部分视图中渲染操作结果。

描述

  1. 您有一个视图,其中使用 RenderAction() 帮助程序显示多个部分视图。
  2. 至少两个部分视图可能通过使用回发到原始视图的 Html.BeginForm() 来呈现
  3. 通过 Ajax 调用执行
  4. 回发数据在表单 POST 时进行验证。

问题

当其中一个表单被发回时,另一个表单呈现为无效

有人使用过这种模式并解决了它吗?我们应该以某种方式知道哪个表单执行了回发,并且只有那个表单应该验证其POST数据。其他人应该忽略验证或执行常规的 HttpVerb.Get 操作处理。

As we all know RenderAction() is either part of:

  • Asp.net MVC 1 Futures or
  • Asp.net MVC 2 Beta 2

and allows us to render action result inside another view or partial view.

Description

  1. You have a view with multiple partial views displayed using RenderAction() helper.
  2. At least two partial views render a <form> probably by using Html.BeginForm() that postback to original view.
  3. Postback is not performed via Ajax call
  4. Postback data is validated upon form POST.

Problem

When one of the forms gets posted back the other one renders as invalid.

Has anyone used this pattern and solved it? We should somehow know which form performed postback and only that one should validate its POST data. Others should either ignore validation or perform regular HttpVerb.Get action processing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏雪无痕 2024-08-19 04:38:40

表单中有一个隐藏字段来指示是哪一个。或者,将前缀传递给部分并附加到表单中的每个元素。

关于前缀。第一种方法是有两个属性,调用 Html.RenderPartial("partial", Model.Data1/2.WithPrefix("data1"))。

public class FormModel
{
   public string Prefix { get; set; }
}

public class FormData
{
   public FormModel Data1 { get; set; }
   public FormModel Data2 { get; set; }
}

public ActionResult HandlePost(FormData data)
{
   if (data.Data1 != null) {} else {}
}

第二种方法相同,但使用两个操作参数。

public ActionResult HandlePost(FormModel data1, FormModel data2)
{
   if (data1 != null) {} else {}
}

在分部视图中

<%= Html.TextBox(Model.Prefix + ".FormModelField", Model.FormModelField) %>

,您可以使用模型中传递的前缀设置字段名称。

当然,您可以在细节上有所不同。

Have a hidden field in the form to indicate which one. Or, have a prefix passed to the partial and appended to each element in the form.

About prefixes. First way is to have two properties, calling Html.RenderPartial("partial", Model.Data1/2.WithPrefix("data1")).

public class FormModel
{
   public string Prefix { get; set; }
}

public class FormData
{
   public FormModel Data1 { get; set; }
   public FormModel Data2 { get; set; }
}

public ActionResult HandlePost(FormData data)
{
   if (data.Data1 != null) {} else {}
}

Second way is the same but use two action parameters.

public ActionResult HandlePost(FormModel data1, FormModel data2)
{
   if (data1 != null) {} else {}
}

In the partial view you do

<%= Html.TextBox(Model.Prefix + ".FormModelField", Model.FormModelField) %>

that is, you set field name with the prefix passed in the model.

Of course you may vary this in details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文