[英]How to send client data to MVC controller for Login page?And save to database?
Login.cshtml Login.cshtml
<script type="text/javascript">
function data() {
var n = document.getElementById("username").value;
var m = document.getElementById("password").value;
?
</script>
<button type="submit"class="btn-login" onClick="Data()">
Giriş Yap </button>
Account Controller 客户总监
DBEntities dB = new DBEntities();
[HttpPost] (username) (password)
public ActionResult Login(string KullaniciAdi,string Sifre)
{
// Session[KullaniciAdi] = dB.Profil.Where(x => x.KullaniciAdi == KullaniciAdi && x.Sifre == Sifre).FirstOrDefault();
var session = (from p in dB.Profil
where p.KullaniciAdi == KullaniciAdi
select p).FirstOrDefault();
return View();
}
My OOP homework is this website and i want to create a login with sending password and username to controller and compare submitted data and the data in the database .Please help :) 我的OOP作业是此网站,我想创建一个登录名,并向控制器发送密码和用户名,并比较提交的数据和数据库中的数据。请帮助:)
You can send client data by using ajax request, 您可以使用ajax请求发送客户端数据,
this is your inputs 这是你的投入
<input id="username" type="text" />
<input id="password" type="password" />
<input id="loginbutton" onclick="UserLogin()" type="submit" value="Submit" />
Submit button bind with UserLogin js function. 提交按钮与UserLogin js函数绑定。
here is the js function. 这是js函数。 we are sending ajax post request here to our controller
我们在这里向我们的控制器发送ajax发布请求
<script type="text/javascript">
function UserLogin() {
var n = document.getElementById("username").value;
var p = document.getElementById("password").value;
var postObj = JSON.stringify({
"username": n,
"password": p
});
$.ajax({
url: "/Home/Login", // endpoint
type: "POST",
data: postObj,
contentType: "application/json; charset=utf-8",
success: function (result) {
// success
},
error: function (errorData) { onError(errorData); }
});
}
</script>
And your controller should be like, you can implement login logic inside of the method. 和您的控制器应该一样,您可以在方法内部实现登录逻辑。
[HttpPost]
public ActionResult Login(string username, string password)
{
// use your code loged in
return Json(true, JsonRequestBehavior.AllowGet);
}
First You Create a Class : 首先,您创建一个类:
public class LoginModel
{
[Required(ErrorMessage = "User Name can not be empty!")]
public string LOGINID { get; set; }
[Required(ErrorMessage = "Password can not be empty!")]
public string LOGINPW { get; set; }
}
Then Your Controller Action Method do this: 然后,您的控制器操作方法将执行此操作:
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
//Here check the Login Id and Password
return View();
}
Now in view Write this. 现在在视图中写这个。 Now when you click the submit button a post call go to the Login Controller with given LOGINID and LOGINPW :
现在,当您单击“提交”按钮时,使用给定的LOGINID和LOGINPW进行登录呼叫:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<!-- login screen -->
<form action="#">
@Html.TextBoxFor(m => m.LOGINID, htmlAttributes: new { @class = "form-control", placeholder = "Login ID" })
@Html.ValidationMessageFor(model => model.LOGINID, "", new { @class = "text-danger", style = "float: left" })
@Html.PasswordFor(m => m.LOGINPW, htmlAttributes: new { @class = "form-control", placeholder = "Password" })
@Html.ValidationMessageFor(model => model.LOGINPW, "", new { @class = "text-danger", style = "float: left" })
<button type="submit" class="btn btn-primary" style="background: #2e6da4; color: #FFF;">Login</button>
</form>
}
@{
var actionURL = Url.Action("Action", "Controller",
FormMethod.Post, Request.Url.Scheme)
+ Request.Url.PathAndQuery;
}
@using (Html.BeginForm("Action", "Controller", FormMethod.Post,
new { @action = actionURL }))
{
<input type="text" name="username">
<input type="text" name="password">
<button type="submit"class="btn-login"></button>
}//Then use Request.Form[0] with the id to get the user name and password in the controller action.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.