简体   繁体   English

使用jquery和asp.net mvc更新字段或重定向页面

[英]update field or redirect page using jquery and asp.net mvc

Im new to jquery and stuck with what i want to achieve. 我是jquery的新手,并且坚持要实现的目标。 Heres what I want to do using jquery and asp.net mvc. 这是我想使用jquery和asp.net mvc做的事情。

  • click a submit button 点击提交按钮
  • this calls an action method called LogOn in the controller Account 这将调用控制器帐户中名为LogOn的操作方法
  • if the call allows users to log in succesfully redirect to a url (sepecified by LogOn) 如果呼叫允许用户成功登录,则重定向到url(由LogOn表示)
  • if it fails replace a div(with id="error") with "sorry error occured" 如果失败,则将div(id为“ error”)替换为“发生抱歉错误”

so far I tried this: 到目前为止,我尝试了这个:

 $("#submit")
            .button()
            .click(function () {
                $.ajax({
                    type: "POST",
                    url: "Account/LogOn",
                    dataType: "json",
                    success: function (data, textStatus) {
                        if (data.redirect) {
                            // data.redirect contains the string URL to redirect to
                            window.location.href = data.redirect;
                        }
                        else {
                            // data.form contains the HTML for the replacement form
                            $("#error2").replaceWith(data.error);
                        }
                    }
                });

            });

how do I construct the relevant bits in the action method? 如何构造动作方法中的相关位? to make this work? 使这项工作? and is the jquery code ok? jQuery代码还可以吗? i suspect prob not. 我怀疑没有。

Thanks 谢谢

If you want to redirect asp.net page at same directory , you can by Jquery/Java script by this : 如果要重定向同一目录中的asp.net页面,可以通过Jquery / Java脚本执行以下操作:

  $("#ctl00_iframecontent_BtnCancle").click(function () {
        window.location = "IncSLAList.aspx?bi=37";
    });

and To redirect to Another page of project , can use : 和重定向到项目的另一页,可以使用:

     window.location.href = "http://ImageUpload/Welcome.aspx?

Your jQuery is almost correct: 您的jQuery几乎是正确的:

  • Don't call .button() (unless you're using jQuery UI and want to do that) 不要调用.button() (除非您正在使用jQuery UI并且想要这样做)
  • Add return false; 添加return false; at the end of the click handler to prevent the browser from submitting normally. click处理程序的末尾,以防止浏览器正常提交。

In the action, you would either return Json(new { redirect = str }) or return Json(new { error = str }) . 在操作中,您将return Json(new { redirect = str })return Json(new { error = str })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM