mvc.net多种形式childaction [英] mvc.net multiple forms childaction
问题描述
我有一个页面上的2种形式,它们包括在像这样的母版:
I have 2 forms on a page, they are included in the masterpage like so:
Html.RenderAction("Form1", "Controller")
和
Html.RenderAction("Form2", "Controller")
该控制器具有以下内容:
The Controller has the following:
<ChildActionOnly()>
Function Form1() As ActionResult
Return View("Form1", New ModelObject())
End Function
<ChildActionOnly()> <AcceptVerbs(HttpVerbs.Post)>
Function Form1(ByVal formCollection As FormCollection) As ActionResult
Return View("Form1", New ModelObject())
End Function
<ChildActionOnly()>
Function Form2() As ActionResult
Return View("Form2", New ModelObject())
End Function
<ChildActionOnly()> <AcceptVerbs(HttpVerbs.Post)>
Function Form2(ByVal formCollection As FormCollection) As ActionResult
Return View("Form2", New ModelObject())
End Function
在ASCX的形式标记如下所示,它们基本上是相同的形式使标记非常相似:
The forms markup in the ascx is as follows, they are essentially the same form so the markup is very similar:
<% Using Html.BeginForm()%>
<%= Html.TextBoxFor(Function(model) model.Property1, New With {.class = "input"})%>
<input type="submit" class="submitbutton" value="" name="submit" />
<% End Using%>
问题是,当我提出这两种形式,它同时运行后的方法。
The problem is, when I submit either form, it runs both post methods.
所以Form1的职位和岗位的Form2,但形式集合中的值从以往的形式提交。
So Form1 post and Form2 post, yet the values in the form collection are from which ever form was submitted.
我的问题是:
这是为什么提交与一组表格数据的两种形式?
我怎样才能使它只调用了正确的表单数据的相关动作?
My question is: Why is this submitting both forms with one set of form data? How can I make it call only the relevant action with the correct form data?
我相信,我想提出一个简单的错误,就是不能看到它好看。
I am sure I am making a simple mistake, just cannot see it for looking.
项目演示该问题可以在这里找到: TestMVC.zip
Project that demonstrates the problem can be found here: TestMVC.zip
先谢谢了。
推荐答案
我已经找到了解决问题的办法,想知道如果有人想在这个解决的真实性发表评论。
I have found a solution to the problem, was wondering if someone would like to comment on the correctness of this "work around".
OK ......所以步骤之一,从岗位操作取出childonlyaction属性,并添加控制器/动作时,提交表单运行。
ok... so step one, Remove the childonlyaction attribute from the post actions and add the controller/action to run when the form is submitted.
Html.BeginForm("Form1", "Form")
这可确保正确的动作后调用。
This makes sure that the correct post action is called.
下一步是制定出什么,我想回去。
The next step was to work out what I wanted to return.
所以..我需要的,如果有验证错误等,所以想我可以做到这一点使用元数据或其他自定义验证,模型添加到的TempData $ C返回自定义模型$ C>,然后做一个
RedirectToAction
使得动作,我所来自的页面。即/首页/索引或/控制器/动作
So.. I need to return the custom model if there are validation errors etc. So thought I could do this using meta data or some other custom validation, add the model to TempData
and then do a RedirectToAction
making the action the page that I came from. i.e. /Home/Index or /Controller/Action
我得到引荐,应始终被设置为此控制器/行动是从一个POST操作来了。
I get the controller/action from the referrer, that should always be set as this is coming from a post action.
谁能想到这样做的更好的办法?由于这是唯一的办法我能找到给我想要的结果,而不使用Ajax
Can anyone think of a better way of doing this?? As this is the only way I could find to give the results I want without using Ajax
这篇关于mvc.net多种形式childaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!