我正在建立一个非常简单的社交网络,这是我的问题:例如,我只想为拥有此配置文件的用户在用户页面上显示一个“更改配置文件图片”按钮,而对于其他授权用户则应该将其隐藏。有什么建议?
这是我的登录方法:
[HttpPost]
public ActionResult Login(LoginModel user)
{
if (ModelState.IsValid)
{
if (IsValid(user.UserName, user.Password))
{
FormsAuthentication.SetAuthCookie(user.UserName, false);
return RedirectToAction("About", "User", new{ username = user.UserName });
}
else
{
ModelState.AddModelError("", "Login data is incorrect");
}
}
return View(user);
}
LoginModel:
public class LoginModel
{
[Required]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 6)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Text)]
[StringLength(100)]
[Display(Name = "User name")]
public string UserName { get; set; }
}
以及用户配置文件的路由:
routes.MapRoute(
name: "User",
url: "{UserName}",
defaults: new
{
controller = "User",
action = "About",
id = UrlParameter.Optional
});
您可以在Controller中的Model中简单地设置一个变量,指示您要显示html的特定部分。
样本:
在模型中添加一个属性:
public TestModel
{
public bool ShowContentX {get;set;}
}
在您的Controller中,将其填充并将模型传递给View:
TestModel t = new TestModel();
t.ShowContentX = true; // create check here.
return View(t);
在您的视图中,检查该属性是否为true
:
@if (@model.ShowContentX) {
<p>add your html</p>
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句