Asp.net mvc 用于在ASP.NET MVC3视图中显示/隐藏链接的变量

Asp.net mvc 用于在ASP.NET MVC3视图中显示/隐藏链接的变量,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,对MVC3来说是相当陌生的,我正在做一个简单的测试项目。所以我遇到了一个小问题,我想请你帮忙 目前我有以下代码:- <p> @if (UserCanCreate) { @Html.ActionLink("Create New", "Create") } @if(UserCanCreate) { @ActionLink(“新建”、“创建”) } 我的问题是,如何从控制器中填充布尔值UserCanCreate?我尝试在控制器中创建属性,但视图仍然看不到这一点 有没有更好的

对MVC3来说是相当陌生的,我正在做一个简单的测试项目。所以我遇到了一个小问题,我想请你帮忙

目前我有以下代码:-

<p>
@if (UserCanCreate)
{
    @Html.ActionLink("Create New", "Create")
}

@if(UserCanCreate)
{
@ActionLink(“新建”、“创建”)
}

我的问题是,如何从控制器中填充布尔值UserCanCreate?我尝试在控制器中创建属性,但视图仍然看不到这一点

有没有更好的办法

谢谢你的帮助

更新为ViewModel

@model IEnumerable<DBName.ViewModels.ProfileData>


@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </td>
    <td>
        @Html.ActionLink("Details", "Details", new { id=item.FantaTeamID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.FantaTeamID })
    </td>
</tr>
@model IEnumerable
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.UserName)
@ActionLink(“详细信息”,“详细信息”,新的{id=item.FantaTeamID})|
@ActionLink(“删除”,“删除”,新的{id=item.FantaTeamID})
}

现在如何替换此代码?从何处获取item.UserName


谢谢

您的视图必须有一个模型,例如UserModel

将UserCanCreate boolean属性添加到UserModel,然后您就可以在视图中访问它了

例如:

namespace MvcApplication1.Models
{
    public class UserModel
    {
        public bool UserCanCreate { get; set; }
    }
}
// ViewModel
public class MyViewModel
{
     public bool UserCanCreate{get;set;}
}

// Controller
public class MyController : Controller
{
     public ActionResult Index()
     {
          var model = new MyViewModel
          {
              UserCanCreate = _permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel)
          };
          return View(model);
     }

     // Other Actions goes here....
}

// View
<p>
@if (Model.UserCanCreate)
{
    @Html.ActionLink("Create New", "Create")
}
并在视图中使用它:

@model MvcApplication1.Models.UserModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        @Model.UserCanCreate
    </div>
</body>
</html>
@model mvcapapplication1.Models.UserModel
@{
布局=空;
}
指数
@Model.UserCanCreate

视图必须有一个模型,例如UserModel

将UserCanCreate boolean属性添加到UserModel,然后您就可以在视图中访问它了

例如:

namespace MvcApplication1.Models
{
    public class UserModel
    {
        public bool UserCanCreate { get; set; }
    }
}
// ViewModel
public class MyViewModel
{
     public bool UserCanCreate{get;set;}
}

// Controller
public class MyController : Controller
{
     public ActionResult Index()
     {
          var model = new MyViewModel
          {
              UserCanCreate = _permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel)
          };
          return View(model);
     }

     // Other Actions goes here....
}

// View
<p>
@if (Model.UserCanCreate)
{
    @Html.ActionLink("Create New", "Create")
}
并在视图中使用它:

@model MvcApplication1.Models.UserModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        @Model.UserCanCreate
    </div>
</body>
</html>
@model mvcapapplication1.Models.UserModel
@{
布局=空;
}
指数
@Model.UserCanCreate

创建模型时,设置值

UserCanCreate = _permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel),

创建模型时,请设置值

UserCanCreate = _permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel),

至少有两种不同的方法可以做到这一点

第一个(更好的)是使用模型传递数据。为此,应创建一个模型类:

//Model.cs
public class MyModel
{
    public bool UserCanCreate { get; set;}
}
public class UsersViewModel
{
    public IEnumerable<DBName.Models.Users> Users { get; set; }
    public bool UserCanCreate { get; set; }
    ... // Any other additional properties you may require
}
并在视图的开头指定它(使用类型化视图):

第二(速度更快,但不推荐):使用ViewData collection传递值

例如,在控制器操作中,执行以下操作:

public ActionResult YourAction()
{
    ...
    ViewData["UserCanCreate"] = ... ; // Determine if link shall be visible here
    ...
    return View();
}
您的视图将如下所示:

...

<p>
@if ((bool)ViewData["UserCanCreate"])
{
    @Html.ActionLink("Create New", "Create")
}
。。。

@if((bool)ViewData[“UserCanCreate”])
{
@ActionLink(“新建”、“创建”)
}
同样重要的是:如果您试图以这种方式保护某些操作,请不要忘记在“创建”操作代码中添加一些权限检查,否则任何用户都可以通过在浏览器地址栏中输入相应的URL来执行操作,即使ActionLink不存在

更新

至于你的评论,你似乎把域模型和视图模型混为一谈。域模型是表示数据库实体的模型,而视图模型是应该用于表示的模型。通常,控制器的工作是将对象从域转换为视图模型,反之亦然

在您的例子中,EF模型是域模型

对于ViewModel,您应该创建一个单独的类,该类将聚合您的域模型实例和您想要的任何其他属性,如UserCanCreate

示例模型类:

//Model.cs
public class MyModel
{
    public bool UserCanCreate { get; set;}
}
public class UsersViewModel
{
    public IEnumerable<DBName.Models.Users> Users { get; set; }
    public bool UserCanCreate { get; set; }
    ... // Any other additional properties you may require
}
公共类UsersViewModel
{
公共IEnumerable用户{get;set;}
公共bool UserCanCreate{get;set;}
…//您可能需要的任何其他属性
}
在控制器中,它将是:

public ActionResult YourAction()
{
    ...
    UsersViewModel model = new UsersViewModel();
    model.Users = ... ; // Get your IEnumerable<DBName.Models.Users> collection from DB and assign it to model property here
    model.UserCanCreate = ... ; // Determine if link shall be visible here
    ...
    return View(model);
}
public ActionResult YourAction()
{
...
UsersViewModel model=新的UsersViewModel();
model.Users=…;//从数据库获取IEnumerable集合,并在此处将其分配给model属性
model.UserCanCreate=…;//确定链接是否在此处可见
...
返回视图(模型);
}
鉴于:

@model %Your_model_namespace%.UsersViewModel

... // use Model.Users wherever you need your IEnumerable<DBName.Models.Users>

<p>
@if (Model.UserCanCreate)
{
    @Html.ActionLink("Create New", "Create")
}
@model%您的\u model\u命名空间%.UsersViewModel
... // 在需要IEnumerable的地方使用Model.Users

@if(Model.UserCanCreate)
{
@ActionLink(“新建”、“创建”)
}

至少有两种不同的方法可以做到这一点

第一个(更好的)是使用模型传递数据。为此,应创建一个模型类:

//Model.cs
public class MyModel
{
    public bool UserCanCreate { get; set;}
}
public class UsersViewModel
{
    public IEnumerable<DBName.Models.Users> Users { get; set; }
    public bool UserCanCreate { get; set; }
    ... // Any other additional properties you may require
}
并在视图的开头指定它(使用类型化视图):

第二(速度更快,但不推荐):使用ViewData collection传递值

例如,在控制器操作中,执行以下操作:

public ActionResult YourAction()
{
    ...
    ViewData["UserCanCreate"] = ... ; // Determine if link shall be visible here
    ...
    return View();
}
您的视图将如下所示:

...

<p>
@if ((bool)ViewData["UserCanCreate"])
{
    @Html.ActionLink("Create New", "Create")
}
。。。

@if((bool)ViewData[“UserCanCreate”])
{
@ActionLink(“新建”、“创建”)
}
同样重要的是:如果您试图以这种方式保护某些操作,请不要忘记在“创建”操作代码中添加一些权限检查,否则任何用户都可以通过在浏览器地址栏中输入相应的URL来执行操作,即使ActionLink不存在

更新

至于你的评论,你似乎把域模型和视图模型混为一谈。域模型是表示数据库实体的模型,而视图模型是应该用于表示的模型。通常,控制器的工作是将对象从域转换为视图模型,反之亦然

在您的例子中,EF模型是域模型

对于ViewModel,您应该创建一个单独的类,该类将聚合您的域模型实例和您想要的任何其他属性,如UserCanCreate

示例模型类:

//Model.cs
public class MyModel
{
    public bool UserCanCreate { get; set;}
}
public class UsersViewModel
{
    public IEnumerable<DBName.Models.Users> Users { get; set; }
    public bool UserCanCreate { get; set; }
    ... // Any other additional properties you may require
}
公共类UsersViewModel
{
公共IEnumerable用户{get;set;}
公共bool UserCanCreate{get;set;}
…//您可能需要的任何其他属性
}
在控制器中,它将是:

public ActionResult YourAction()
{
    ...
    UsersViewModel model = new UsersViewModel();
    model.Users = ... ; // Get your IEnumerable<DBName.Models.Users> collection from DB and assign it to model property here
    model.UserCanCreate = ... ; // Determine if link shall be visible here
    ...
    return View(model);
}
public ActionResult YourAction()
{
...
UsersViewModel model=新的UsersViewModel();
model.Users=…;//从数据库获取IEnumerable集合,并在此处将其分配给model属性
model.UserCanCreate=…;//确定链接是否在此处可见
...
返回视图(模型);
}
鉴于:

@model %Your_model_namespace%.UsersViewModel

... // use Model.Users wherever you need your IEnumerable<DBName.Models.Users>

<p>
@if (Model.UserCanCreate)
{
    @Html.ActionLink("Create New", "Create")
}
@model%您的\u model\u名称