2016-04-25 44 views
0

我有一个ASP.NET MVC5网络应用程序。用户可以添加项目(数据库条目),项目还存储添加项目的用户的userId。我已经在控制器中添加了限制,不会让其他用户删除或编辑您的条目,仅限于您。ASP.NET Razor .cshtml文件在控制器中添加“if”检查?

这里是如何看起来控制器:

public ActionResult Edit([Bind(Include = "ProjectId,UserId,Title,ApplicationDeadline,Duration,HourlyRate,TotalProjectCost,City,RequiredPresencePercent,Language,RequiredSkills,Description")] Project project) 
     { 
      if (ModelState.IsValid) 
      { 
       if (project.UserId == User.Identity.GetUserId()) 
       { 
       db.Entry(project).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
       } 

       return HttpNotFound("Project is not yours!"); 

      } 
      return View(project); 
     } 

但在查看还有一个ActionLink的

@Html.ActionLink("Edit", "Edit", new {id = item.ProjectId}) 

我如何添加这个.cshtml文件,编辑内部检查/删除actionlinks甚至不会出现在不属于你的项目附近?

我试过@if (project.UserId == User.Identity.GetUserId())但当然这里没有定义project

编辑:找到解决方案感谢斯蒂芬

@if(item.UserId == User.Identity.GetUserId()) 

      { 
       @Html.ActionLink("Edit", "Edit", new {id = item.ProjectId}) 
       @Html.ActionLink("Delete", "Delete", new {id = item.ProjectId}) 
      } 
+0

使用Model.Project – CodeLover

+0

什么是页面的模型?以及为什么项目没有在页面中定义。如果项目可用,您可以使用if语句简单地包装@ Html.ActionLink(“Edit”,“Edit”,new {id = item.ProjectId})。我看到Edit action方法返回View(Project)这个视图是什么,这个视图与你提到的页面不同。 – NatarajC

+0

:'IEnumerable '不包含'project'的定义,并且没有找到接受类型为'IEnumerable '的第一个参数的扩展方法'project'(您是否缺少using指令或程序集引用?它应该有点不同? – crystyxn

回答

0

使用Model.project像

@if(Model.project) 
0

您可以使用此:

@if(Model.project.UserId == User.Identity.GetUserId()) { 
    Html.ActionLink("Edit", "Edit", new {id = item.ProjectId}) 
} 
相关问题