如何从MVC中的视图页面调用操作 [英] How to call action from view page in MVC

查看:102
本文介绍了如何从MVC中的视图页面调用操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在每个循环中都有一个按钮,如果我点击按钮,则使用java脚本显示弹出窗口。也可以在java脚本中查看视图。在弹出窗口中,另一个按钮是订阅按钮。如果我点击订阅需要回家控制器动作方法。



我尝试过:



在这里我使用

 form action =@ Url.Action(Superoffer)method =POST

来调用动作但不工作



< button  class  =  计划按钮演示>选择计划< /  按钮 >  





< pre lang =Javascript>< form action = @ Url.Action( Superoffer me thod = POST>
< script src = https://checkout.stripe.com/checkout.js>< /脚本>
< script>
var handler = StripeCheckout.configure({
key:
image: https:// stripe。 com / img / documentation / checkout / marketplace.png
name: Abacus
说明: Pro订阅套餐
panelLabel : 订阅
allowRememberMe: false
});
var el = document .getElementsByClassName( demo);
for var i = 0 ; i< el.length; i ++){
el [i] .addEventListener(' click ' function (e){
handler.open();
e.preventDefault();
} );
}
// document.getElementsByClassName(plan-button)。addEventListener('click ',function(e){
// handler.open();
// e.preventDefault();
// });
< / script>

< div class = 拼写> ABACUS < / div >

< / 表格 >

解决方案

一些事情

- 您忘记包含所需的API'密钥'

- 您忘记了货币和金额,我还设置了区域设置:'auto'

- 你还记得顶部的jquery吗?

- 当用户批准付款时你应该指定一个令牌部件功能



见实例: stripe.com/docs [ ^ ]



i没有得到你的表单部分,如果你使用'@',那是否意味着你想使用Razor viewengine?

如果是这样,表格就会这样开始:

 @using(Html.BeginForm(Action,Controller,FormMethod.Post))
{



您想使用常规的html表单吗? ?在这种情况下,你不应该尝试发布到控制器,因为post是一个基于页面的概念,而是你应该抓住提交按钮事件并做一个ajax帖子,因为你必须在提交它之前在客户端获得tripe结果如果你专门使用条纹,不要考虑直接发布值。



< form id =funnyform> 

< input type =buttonid =mysubmitvalue =submit />

< script type =javascript>
document.getElementById('mysubmit')。addEventListener('click',function(e){
//使用条形$调用的表单和/或return方法向控制器发送ajax帖子b $ b e.preventDefault();
});
< / script>
< / form>



示例ajax post

asp.net mvc - MVC ajax json发布到控制器操作方法 [ ^ ]


I have a button inside for each loop,if i click the button display the popup using java script.Pop up view also in java script.In popup window another button is there"Subscribe button".If i click subscribe need to go home controller action method.

What I have tried:

Here i am using

form action="@Url.Action("Superoffer")" method="POST"

for calling the action but not working

<button class="plan-button demo" >Choose Plan</button>



<form action="@Url.Action("Superoffer")" method="POST">
            <script src="https://checkout.stripe.com/checkout.js"></script>
            <script>
                var handler = StripeCheckout.configure({
                    key: "",
                    image: "https://stripe.com/img/documentation/checkout/marketplace.png",
                    name: "Abacus",
                    description: "Pro Subscription Package",
                    panelLabel: "Subscribe",
                    allowRememberMe: false
                });
                var el = document.getElementsByClassName("demo");
                for (var i = 0; i < el.length; i++) {
                    el[i].addEventListener('click', function (e) {
                        handler.open();
                        e.preventDefault();
                    });
                }
                //document.getElementsByClassName("plan-button").addEventListener('click', function (e) {
                //    handler.open();
                //    e.preventDefault();
                //});
            </script>

            <div class="spelled-out">ABACUS</div>

</form>

解决方案

A couple of things
- You forgot to include your API 'key' that is required
- you forgot currency and amount and i'd also set locale: 'auto'
- did you remember jquery in the top?
- You should specify a token part function to be called when user has approved payment

See for instance: stripe.com/docs[^]

i don't get your form part, if you use '@' does that mean you wanna use Razor viewengine?
If so a form would start something like this:

@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{


Would you like to use a regular html form? in that case you shouldn't attempt to post to a controller as post is a page based concept, instead you should catch the submit button event and do an ajax post, because you have to get the tripe result on the client before submitting it doesn't add value to consider posting directly if you're using stripe specifically.

<form id="funnyform">

<input type="button" id="mysubmit" value="submit/>

<script type="javascript">
 document.getElementById('mysubmit').addEventListener('click', function (e) {
           //Do an ajax post to your controller with the values you want from your form and/ or return method invoked by stripe
            e.preventDefault();
        });
</script>
</form>


Example of ajax post
asp.net mvc - MVC ajax json post to controller action method[^]


这篇关于如何从MVC中的视图页面调用操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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