如何针对不同的部分表单提交获得不同的MVC操作结果 [英] How to get different MVC actionresults for different partial form submissions

查看:88
本文介绍了如何针对不同的部分表单提交获得不同的MVC操作结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在index.cshtml中有两个部分,例如:

@ Html.Partial(_ EnquiryForm,new SharedModels.SC.Enquiry())

@Html .Partial(_ MemberLoginForm,新的SharedModels.SC.User())



有谁知道为什么表单提交继续进入Index actionresult而不是他们自己行动结果?



我尝试了什么:



两个部分都有形式其中的输入,例如:

< form>< input type =submitid =_ MemberLoginFormname =_ MemberLoginFormvalue =Login>< / form>



< form>< input type =submitid =submitname =_ EnquiryFormvalue =Submit>< / form>



但两种形式总是去

[HttpPost]

公共ActionResult指数(查询查询)

{}



而不是我期望的:

[HttpPost]

public A. ctionResult _EnquiryForm(查询查询)

{}



[HttpPost]

公共ActionResult _MemberLoginForm(用户用户) )

{}

I have two partials inside index.cshtml such as:
@Html.Partial("_EnquiryForm", new SharedModels.SC.Enquiry())
@Html.Partial("_MemberLoginForm", new SharedModels.SC.User())

Does anyone know why the form submissions keep going to the Index actionresult instead of to their own actionresults?

What I have tried:

both partials have form inputs in them, such as:
<form><input type="submit" id="_MemberLoginForm" name="_MemberLoginForm" value="Login" ></form>
and
<form><input type="submit" id="submit" name="_EnquiryForm" value="Submit" ></form>

But both forms always go to
[HttpPost]
public ActionResult Index(Enquiry enquiry)
{}

instead of to what I'd expect:
[HttpPost]
public ActionResult _EnquiryForm(Enquiry enquiry)
{}
or
[HttpPost]
public ActionResult _MemberLoginForm(User user)
{}

推荐答案

数据发送到的URL由表单的action属性控制,因为你没有action属性默认为当前url,因此这将是您的Index操作。您需要在每个局部视图中为表单标记指定一个操作属性,该属性指向您希望该表单提交到的操作。您可以使用表单标记内的@ Url.Action帮助程序来完成此操作;



< form action =@ Url.Action(...)> ;



或者你可以使用BeginForm帮助器为你制作整个表格标签



FormExtensions.BeginForm Method (System.Web .Mvc.Html) [ ^ ]
The url the data is sent to is governed by the form's "action" attribute, and as you have no action attribute it defaults to the current url, so that will be your Index action. You need to give the form tag in each partial view an action attribute that points to the action you want that form to submit to. You can do this by using the @Url.Action helper inside the form tag;

<form action="@Url.Action( ... )">

or you can use the BeginForm helper to make the whole form tag for you

FormExtensions.BeginForm Method (System.Web.Mvc.Html)[^]


谢谢,



我使用

< form method =postaction =@ Url.Action(_ MemberLoginForm,Home)>< / form>
Thanks,

I used
<form method="post" action="@Url.Action("_MemberLoginForm", "Home")"></form>


这篇关于如何针对不同的部分表单提交获得不同的MVC操作结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆