C# MVC5如何使用控制器中的方法/条件渲染/创建/更改按钮颜色?
我想通过使用条件动态呈现网页的按钮。我一直在想控制器中应该有这样的东西:C# MVC5如何使用控制器中的方法/条件渲染/创建/更改按钮颜色?,c#,html,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Html,Asp.net,Asp.net Mvc,Asp.net Mvc 5,我想通过使用条件动态呈现网页的按钮。我一直在想控制器中应该有这样的东西: public bool checkUser(){ if (User.Identity.Name == "Bob"){ return true; }else{ return false; } } 在我看来,我想做一些类似的事情: @if (checkUser){ <type = "button".....></>// create a
public bool checkUser(){
if (User.Identity.Name == "Bob"){
return true;
}else{
return false;
}
}
在我看来,我想做一些类似的事情:
@if (checkUser){
<type = "button".....></>// create a red button
}else{
<type = "button".....></>// create a blue button
}
@if(检查用户){
//创建一个红色按钮
}否则{
//创建一个蓝色按钮
}
但我不确定如何将方法的布尔结果传递到视图中。我希望我能够正确地解释我的问题,非常感谢您的帮助。使用
查看包
在操作中指定值
public ActionResult Index() {
bool checkUser;
if (User.Identity.Name == "Bob"){
checkUser = true;
}else{
checkUser = false;
}
ViewBag.checkUser = checkUser;
return View();
}
并使用相同的键在视图中访问它
@if ((bool)ViewBag.checkUser){
<type = "button".....></>// create a red button
}else{
<type = "button".....></>// create a blue button
}
@if((bool)ViewBag.checkUser){
//创建一个红色按钮
}否则{
//创建一个蓝色按钮
}
使用视图包
。在动作中指定值,并使用相同的键在视图中访问它