我正在学习MVC,并且已经在进行一项繁重的工作,试图找出如何在jQuery手风琴选项卡中从局部视图呈现登录表单的方式,而不提交整个页面。
我包括我的页面图像。
您的订单标签
完成订单标签
第一个标签:“您的订单”在右侧包含一个小的登录表单,带有一个显示“登录”的按钮。
第五个标签:“完成订单”包含一个按钮“完成订单”
When I click the button on the first tab to Sign In, the code that executes is the code for Complete Order button, and that causes many errors, because all the information collected on subsequent tabs is not there as when Complete Order gets clicked.
I would like to ask for help, to figure out a way to separate the events of these buttons, and be able to do a log in without running code that is part of other event.
I am not sure how to approach to this, this is my first time working with this architecture.
If I could get help on how to execute the log in method, and just display a simple div displaying "success! that would help me a lot.
Thank you much!
CODE EDIT AFTER RECOMMENDATIONS
The jQuery:
function ShoppingCartLogin() {
$("#Email").val();
var userid = $("#YourOrder_MainLoginEmail").val();
//var userid = document.getElementById('UserName').value;
var password = $("#YourOrder_MainLoginPassword").val();
//var password = document.getElementById('Password').value;
alert(userid);
var url = "/ShoppingCart/ShoppingCartLogin";
$("#btnLogin").val('Sign In');
$.ajax({
url: url,
data: { userId: userid, pass: password },
cache: false,
type: "POST",
success: function (data) {
if (data == "1") {
alert("Successfull login.");
} else {
alert("Invalid user id and password.");
}
$(".YourOrder_MainLoginEmail").attr({ 'value': '' });
$(".YourOrder_MainLoginPassword").attr({ 'value': '' });
},
error: function (reponse) {
alert("error : " + reponse);
}
});
$("#btnlogin").val('Sign In');
}
The PartialView markup
<div class="YourOrder__inputEmail">
Html.TextBoxFor(m => m.UserName, new { @class = "YourOrder_MainLoginEmail", placeholder = "Email
Address", @id = "YourOrder_MainLoginEmail" })
</div>
<div>
<div>
Html.PasswordFor(m => m.Password, new { @class = "YourOrder_MainLoginPassword", placeholder =
"Password", @id = "YourOrder_MainLoginPassword" })
</div>
</div>
The controller code:
public ActionResult ShoppingCartLogin(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
using (Entities db = new Entities())
{
var user = (from u in db.Users where u.Email == model.UserName && u.Password == model.Password && u.IsActive select u).FirstOrDefault();
if (user == null)
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
List<string> roles = new List<string>();
if (user.IsAdmin)
{
roles.Add("Admin");
}
if (user.IsOrdersAdmin)
{
roles.Add("Orders");
}
if (user.IsStoreAdmin)
{
roles.Add("Stores");
}
user.LastLoginDate = DateTime.Now;
db.SaveChanges();
FormsAuthentication.SetAuthCookie(user.ID.ToString(), model.RememberMe);
var authTicket = new FormsAuthenticationTicket(
1, // version
user.ID.ToString(), // user name
DateTime.Now, // created
DateTime.Now.AddMinutes(90), // expires
model.RememberMe, // persistent?
string.Join(",", roles) // can be used to store roles
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
HttpContext.Response.Cookies.Add(authCookie);
}
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
执行代码后,尽管文本框中有值,但LoginModel的属性UserName和Password为null。
该代码转到:
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
对于分隔两个按钮,放置的两个按钮两个不同的ID,假设标签0(你的顺序)btnYourOrder和标签5(完成订单)按钮,ID为btnCompleteOrder的按钮ID,然后在你写两个jQuery的单击事件$("#btnYourOrder").click(function(){<Your Code>});
和$("#btnCompleteOrder").click(function(){<Your Code>});
现在任何你想执行,您可以放置其按钮事件。
对于执行部分视图(不是完整的回发),您可以使用以下代码
$.ajax(
{
url: <Your partial View URL>,
type: "POST",
contentType: 'text/html',
success: function (data) {
$("#divOrders").html(data);
},
error: function (ex) {
}
}
);
divOrders->将一个ID为divOrders的div放在要显示部分视图数据的位置。
如果您需要任何其他帮助,或者如果您不了解,请告诉我,我将竭诚为您服务。
感谢Raviranjan
Other options:
Hi Nima: there are many options, Option 1: Change your controller method as
public ActionResult ShoppingCartLogin(LoginModel model, string returnUrl)
--> public ActionResult ShoppingCartLogin(string userId, string pass, string returnUrl)
for your current javascript/juery ajax call .
option 2: Put your User Id, Password, and button(no submit):
@using (Ajax.BeginForm("/ShoppingCartLogin", "ShoppingCart", new AjaxOptions {
UpdateTargetId = "divSaveUsers", InsertionMode =InsertionMode.Replace,
OnSuccess = "onUserSaveSuccess", OnFailure = "onUserSaveFailure", HttpMethod = "POST" },
new { @autocomplete = "off", @id = "frmuserdetails" }))
{<br/>
@Html.AntiForgeryToken()<br/>
@Html.ValidationSummary(true) <br/>
<input type="button" value="Save" id="btnSaveData" /><br/>
}
<br/>
<script>
$("#btnSaveData").click(function (event) {
$("#frmuserdetails").submit();
});<br/>
</script>
and keep your controller method as usual
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句