在我的登录页面上,我有一个下拉列表来更改我正在进行Ajax调用以设置文化的应用程序的文化。默认情况下,我设置为'en_US'。
我的问题是当我直接登录而不改变我能够成功登录的文化时,但是当我改变文化并尝试登录时,我无法做到这一点。这是因为AJAX调用而发生的,这使得自定义属性没有注册?
另外,我的登录方法定义了自定义属性。以下是代码。
AJAX致电
$('#ddlLanguages').change(function () {
var val = $('#ddlLanguages').val()
createCookie('culturecookie', val, 7);
$.ajax({
type: "POST",
url: '/Account/GetCultureNew',
data: { culturename: val },
success: function (result) {
$("#logo-group").html('');
$(document.body).html('');
$(document.body).html(result);
},
error: function (data) {
//alert('Error');
}
});
});
控制器中的Ajax方法
[HttpPost]
public ActionResult GetCultureNew(string culturename)
{
if (!string.IsNullOrEmpty(culturename) & culturename.Contains("#"))
{
string[] strdata = culturename.Split('#');
if (strdata.Length > 0)
{
AppTenant tenant = HttpContext.Session.GetObjectFromJson<AppTenant>("TenantInfo");
if (tenant != null)
{
tenant.LoggedInCulture = strdata[0];
tenant.LanguageID = Convert.ToInt32(strdata[1]);
HttpContext.Session.SetObjectAsJson("TenantInfo", tenant);
}
}
}
List<SelectListItem> items = new List<SelectListItem>();
items = HttpContext.Session.GetObjectFromJson<List<SelectListItem>>("LanguageData");
foreach (var item in items)
{
if (item.Value == culturename)
{
item.Selected = true;
}
else
{
item.Selected = false;
}
}
var itemsString = JsonConvert.SerializeObject(items);
CookieOptions obj = new CookieOptions();
obj.Expires = DateTime.Now.AddMonths(3);
Response.Cookies.Append("Languagelist", itemsString, obj);
var viewModel = new LMS_User { ReturnUrl = string.Empty, LanguageList = items };
return View("Login", viewModel);
}
登录方式
[HttpPost]
[AllowAnonymous]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
[Route("Admin/Login/{clietname}")]
public async Task<IActionResult> Login([Bind(include: "Email,Password,RememberMe")] LMS_User model, string returnUrl)
{
// my login logic
}
编辑: - 1 登录部分视图
<div class="col-xs-12 col-sm-12 col-md-5 col-lg-4">
<div class="well no-padding">
<form action="@Url.Action("login", "account")" method="POST" id="login-form" class="smart-form client-form">
<header>
@obj["SingnIn"]
</header>
@Html.AntiForgeryToken()
<fieldset>
<section>
<label asp-for="LanguageList">@obj["LanguageList"] </label>
@Html.DropDownList("Languages", Model.LanguageList, null, new { id = "ddlLanguages", @class = "form-control" })
</section>
<section>
<label asp-for="Email">@obj["Email"]</label>
<label class="input">
<i class="icon-append fa fa-user"></i>
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
<b class="tooltip tooltip-top-right"><i class="fa fa-user txt-color-teal"></i>>@obj["tooltipEmail"]</b>
<span asp-validation-for="Email" class="text-danger"></span>
</label>
</section>
<section>
<label asp-for="Password">@obj["Password"]</label>
<label class="input">
<i class="icon-append fa fa-lock"></i>
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
<b class="tooltip tooltip-top-right"><i class="fa fa-lock txt-color-teal"></i>@obj["tooltippassword"] </b>
<span asp-validation-for="Password" class="text-danger"></span>
</label>
<div class="note">
<a href="@Url.Action("forgotpassword", "account")"><i class="fa fa-frown-o"></i> @obj["Forgot_password?"]</a>
</div>
</section>
<section>
<label class="checkbox">
<input asp-for="RememberMe" />
<i></i>@obj["Remember_Me"]
</label>
</section>
<footer>
<button type="submit" class="btn btn-primary">
@obj["SingnIn"]
</button>
</footer>
</fieldset>
</form>
</div>
@{ await Html.RenderPartialAsync("_SocialMedia"); }
编辑2: - 完整的登录视图
<div id="content" class="container">
<div class="row">
@{ await Html.RenderPartialAsync("_LoginText"); }
@{ await Html.RenderPartialAsync("_LoginPartial"); }
</div>
</div>
但是,如果我在AJAX成功函数中添加location.reload()
,那么通过更改文化我可以成功登录。
对此有任何帮助表示赞赏!
答案 0 :(得分:1)
执行$(document.body).html(result);
表单的操作部分缺失。因此它不知道在哪里发帖。
希望我得到了一些帮助:)