C# 从MVC查询SQL
目标:我希望能够按下提交按钮,例如:C# 从MVC查询SQL,c#,asp.net-mvc,stored-procedures,C#,Asp.net Mvc,Stored Procedures,目标:我希望能够按下提交按钮,例如: <input type="submit" value="Login" class="btn btn-default" /> Login.cshml @using Orientation.Models @model LoginViewModel @{ ViewBag.Title = "Log in"; } <script src="~/Scripts/jquery-1.10.2.min.js"></script>
<input type="submit" value="Login" class="btn btn-default" />
Login.cshml
@using Orientation.Models
@model LoginViewModel
@{
ViewBag.Title = "Log in";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<h2>@ViewBag.Title.</h2>
<div class="row">
<div class="col-md-8">
<section id="loginForm">
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Use a local account to log in.</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
<p>
@Html.ActionLink("Register as a new user", "Register")
</p>
@* Enable this once you have account confirmation enabled for password reset functionality
<p>
@Html.ActionLink("Forgot your password?", "ForgotPassword")
</p>*@
}
</section>
</div>
<div class="col-md-4">
<section id="socialLoginForm">
@Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
</section>
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jquery")
}
<script>
//$document.ready(function () {
// $("myModal").modal({
// backdrop: 'static',
// });
//});
$("login").click(function ()
{
var dataObject = {Username: $("#Username").val, Password:$("#Password").val};
$.ajax({
url:'url.action("Login","User)',
type: "POST",
data: dataObject,
datatype:"json",
success: function(result)
{
if(result.tostring()=="Success")
{
alert(result);
}
else
{
alert(result);
}
},
error: function(result)
{
alert("Error");
}
});
})
</script>
使用Orientation.Models
@模型LoginView模型
@{
ViewBag.Title=“登录”;
}
@ViewBag.Title。
@使用(Html.BeginForm(“Login”,“Account”,new{ReturnUrl=ViewBag.ReturnUrl},FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
使用本地帐户登录。
@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(m=>m.Email,新的{@class=“col-md-2控制标签”}) @TextBoxFor(m=>m.Email,新的{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Email,“,new{@class=“text danger”}) @LabelFor(m=>m.Password,新的{@class=“col-md-2控制标签”}) @Html.PasswordFor(m=>m.Password,新的{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Password,“,new{@class=“text danger”}) @CheckBoxFor(m=>m.RememberMe) @LabelFor(m=>m.RememberMe) @ActionLink(“注册为新用户”、“注册”) @*为密码重置功能启用帐户确认后启用此选项 @ActionLink(“忘记密码?”,“放弃密码”) *@ } @Html.Partial(“_externalLoginListPartial”,新的ExternalLoginListViewModel{ReturnUrl=ViewBag.ReturnUrl}) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) @Scripts.Render(“~/bundles/jquery”) } //$document.ready(函数(){ //$(“myModal”).modal({ //背景:“静态”, // }); //}); $(“登录”)。单击(函数() { var dataObject={Username:$(“#Username”).val,Password:$(“#Password”).val}; $.ajax({ url:'url.action(“登录”、“用户”), 类型:“POST”, 数据:数据对象, 数据类型:“json”, 成功:功能(结果) { if(result.tostring()=“Success”) { 警报(结果); } 其他的 { 警报(结果); } }, 错误:函数(结果) { 警报(“错误”); } }); })
首先,您的jQuery选择器是错误的。页面中没有名为
login
的元素。请为提交按钮提供id
,以便将其用作jQuery选择器
<input type="submit" id="loginBtn" value="Login" class="btn btn-default" />
现在在登录操作方法中,调用自定义方法并使用该方法的返回值将JSON对象返回给客户端
[HttpPost]
public ActionResult Login(User user)
{
try
{
var bl=new UserBusinessLogic();
var result = bl.CheckUserLogin(user);
//based on the result return something
if(result==1) //demo purpose. Change to match with what your code actually returns
{
return Json(new {Status="Success" });
}
return Json(new {Status==="Invalid", Message="Invalid user credentials" });
}
catch(Exception ex)
{
return Json(new {Status==="Valid", Message="Code crashed!.
Put a breakpoint in your action method and see what is happening" });
}
}
哎呀。那没用。我明天会修改的。你什么意思,那没用。我是说我没帮自己,抱歉。我起床后几个小时会看一看。
$(function(){
$("#loginBtn").click(function(e){
e.preventDefault();
var _form=$(this).closest("form");
$.post(_form.attr("action"),_form.serialize(),function(response){
if(reponse.Status==="Success")
{
alert("Valid user");
}
else
{
alert(response.Message);
}
});
});
});
[HttpPost]
public ActionResult Login(User user)
{
try
{
var bl=new UserBusinessLogic();
var result = bl.CheckUserLogin(user);
//based on the result return something
if(result==1) //demo purpose. Change to match with what your code actually returns
{
return Json(new {Status="Success" });
}
return Json(new {Status==="Invalid", Message="Invalid user credentials" });
}
catch(Exception ex)
{
return Json(new {Status==="Valid", Message="Code crashed!.
Put a breakpoint in your action method and see what is happening" });
}
}