C# 在mvc3中使用ajax表单登录后显示用户的用户名 我在mvc3剃须刀公司工作
我有一个partialview组成的表单,显示为模态框:C# 在mvc3中使用ajax表单登录后显示用户的用户名 我在mvc3剃须刀公司工作,c#,asp.net,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Razor,我有一个partialview组成的表单,显示为模态框: @using (Ajax.BeginForm("Login", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId ="result" , OnComplete = "onComplate()"})) { //forms } 我已在layout.cshtml中将用户名显示为会话[“username”
@using (Ajax.BeginForm("Login", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId ="result" , OnComplete = "onComplate()"}))
{
//forms
}
我假设在刷新页面时会显示用户名 Ajax将只更新目标部分。在您的例子中是'result'div。您还需要刷新会话[“username”]父div
首先。在登录后操作中,首先检查用户是否有效。此时,如果用户有效,请登录,并将其
用户名保存在会话或cookie中。它用于始终在_布局中显示用户名
第二步。如果要在单击登录按钮后在onComplete()函数中显示用户名
OnComplete = "myOncomplete('#usernameDisplayerElement')"
及
在ajax调用中,您始终可以将用户名作为ContentResult
返回,并将其更新为现有div,如下所示:
[HttpPost]
public ContentResult Login(string username, string password)
{
// Your login logic, assuming it sets the below user identity
return Content(string.Format("Hello {0}", HttpContext.User.Identity.Name));
}
您想在登录表单中还是始终在布局中显示登录后的用户名?
[HttpPost]
public ContentResult Login(string username, string password)
{
// Your login logic, assuming it sets the below user identity
return Content(string.Format("Hello {0}", HttpContext.User.Identity.Name));
}