如何在mvc4中单击按钮后停止刷新页面? [英] How to stop the page refreshing after button click in mvc4?

查看:118
本文介绍了如何在mvc4中单击按钮后停止刷新页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要什么



我需要在用户点击提交按钮时停止页面刷新,并且它不会重定向到MVC中的另一个页面。



我还做了什么

使用jquery.if登录页面点击页面上的提交按钮刷新并显示带有用户名的欢迎消息并登录按钮更改为注销链接。但是我想要相同的功能而不刷新页面



我尝试过:



控制器:

[HttpPost]

[ValidateAntiForgeryToken]

公共ActionResult索引(LoginMVC u){



if(ModelState.IsValid)//这是检查有效性

{

使用(LoginAndSignUpEntities dc = new LoginAndSignUpEntities()){

var v = dc.tbl_Detailstbl.Where(a => a.Email_Id.Equals(u.EmailId)&& a.Password.Equals(u.Password))。FirstOrDefault() ;

if(v!= null){

Session [LogedUserID] = v.Email_Id.ToString();

Session [ LogedUserFullname] = v.Name.ToString();

}

}

}

返回Json(u,JsonRequestBehavior.AllowGet);

}

查看:

< button id =modelstyle =border:none;> login< / button>





@using(Html.BeginForm(Index,Home,FormMethod.Post,new { @id =ID})){@ Html.AntiForgeryToken()//这是为了防止CSRF攻击@ Html.ValidationSummary(true)



登录



@ Html.LabelFor(m => m.EmailId)







@ Html.TextBoxFor(m => m.EmailId,new {@id =txtuserid})@ Html.ValidationMessageFor(m => m.EmailId)





@ Html.LabelFor(m => m.Password)





@ Html.PasswordFor( m => m.Password,new {@id =txtpassword})@ Html.ValidationMessageFor(m => m.Password)



< input type =submitvalue =Submitid =btnlogin/>





}





脚本:

@section scripts {

< script type =text / javascript>

$(document).ready(function(){

调试器;

$(#btnlogin)。submit(function(e){

e.preventDefault();

var email = $('#txtuserid')。val();

var password = $('#txtpassword')。val();

$ .ajax({

url:'@ Url.Action(Index,Home)',

类型:'POST',

数据:$ (this).serialize(),

数据类型:json,

成功:function(){

alert(success) ;

},

错误:function(){

a lert(error123);

}

});



});

});

< / script>





弹出脚本:

$( function(){

$(#login_page)。dialog({

autoOpen:false,

width:300,
身高:100,

显示:{

效果:盲目,

持续时间:1000

},

隐藏:{

效果:爆炸,

持续时间:1000

} ,

});

$(#model)。click(function(){

$(#login_page)。对话框(打开);

});

});

请给我一个解决这个问题的方法...... div class =h2_lin>解决方案

(document).ready(function(){

debugger;


(#btnlogin)。submit(function(e){

e.preventDefault();

var email =


('#txtuserid')。val();

var password =


What I need

I need to stop the page refreshing while user click submit button and it doesn't redirect to another page in MVC.

Still what I done
log in page with refreshing using jquery.if click a submit button on the page is refreshing and it display the welcome message with username and login button changed to logout link. but I want the same functionality without refreshing the page

What I have tried:

Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(LoginMVC u) {

if (ModelState.IsValid) // this is check validity
{
using(LoginAndSignUpEntities dc = new LoginAndSignUpEntities()) {
var v = dc.tbl_Detailstbl.Where(a => a.Email_Id.Equals(u.EmailId) && a.Password.Equals(u.Password)).FirstOrDefault();
if (v != null) {
Session["LogedUserID"] = v.Email_Id.ToString();
Session["LogedUserFullname"] = v.Name.ToString();
}
}
}
return Json(u, JsonRequestBehavior.AllowGet);
}
View:
<button id="model" style="border:none;">login</button>


@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "ID" })) { @Html.AntiForgeryToken() // this is for prevent CSRF attack @Html.ValidationSummary(true)


Login


@Html.LabelFor(m => m.EmailId)




@Html.TextBoxFor(m => m.EmailId, new { @id = "txtuserid" }) @Html.ValidationMessageFor(m => m.EmailId)



@Html.LabelFor(m => m.Password)



@Html.PasswordFor(m => m.Password, new { @id = "txtpassword" }) @Html.ValidationMessageFor(m => m.Password)


<input type="submit" value="Submit" id="btnlogin" />



}



Script:
@section scripts{
<script type="text/javascript">
$(document).ready(function() {
debugger;
$("#btnlogin").submit(function(e) {
e.preventDefault();
var email = $('#txtuserid').val();
var password = $('#txtpassword').val();
$.ajax({
url: '@Url.Action("Index", "Home")',
type: 'POST',
data: $(this).serialize(),
datatype: "json",
success: function() {
alert("success");
},
error: function() {
alert("error123");
}
});

});
});
</script>


Popup Script:
$(function() {
$("#login_page").dialog({
autoOpen: false,
width: 300,
height: 100,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "explode",
duration: 1000
},
});
$("#model").click(function() {
$("#login_page").dialog("open");
});
});
please give me a solution for this issue....

解决方案

(document).ready(function() {
debugger;


("#btnlogin").submit(function(e) {
e.preventDefault();
var email =


('#txtuserid').val();
var password =


这篇关于如何在mvc4中单击按钮后停止刷新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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