隐藏网络网格列“编辑"; MVC3中基于条件的链接 [英] Hide webgrid column "Edit" link based on condition in MVC3

查看:52
本文介绍了隐藏网络网格列“编辑"; MVC3中基于条件的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络网格MVC3,其中包含4列名称,地址,年龄&编辑.如果年龄大于55岁,我想隐藏Edit行的链接.请帮我做.

I have a webgrid MVC3 contains 4 columns Name, Address, age & Edit. I want to hide Edit link for row if age is greater than 55. Help me to do it.

是否有类似OnItemDataBound事件的事件?

Is there any event like OnItemDataBound event?

谢谢

推荐答案

是否有类似OnItemDataBound事件的事件?

Is there any event like OnItemDataBound event?

不,在ASP.NET MVC中没有事件这样的概念.

No, there is no such concept as events in ASP.NET MVC.

您可以使用自定义格式列.

You could use a custom format column.

型号:

public class PersonViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var persons = new[]
        {
            new PersonViewModel { Id = 1, Name = "u 1", Address = "a 1", Age = 54 },
            new PersonViewModel { Id = 2, Name = "u 2", Address = "a 2", Age = 55 },
            new PersonViewModel { Id = 3, Name = "u 3", Address = "a 3", Age = 56 },
        };
        return View(persons);
    }
}

查看:

@model IEnumerable<PersonViewModel>
@{
    var grid = new WebGrid(Model);
}

@grid.GetHtml(
    columns: grid.Columns(
        grid.Column("Name"),
        grid.Column("Address"),
        grid.Column("Age"),
        grid.Column(
            header: "Edit", 
            format: 
                @<text>
                @if (item.Age < 56) 
                { 
                    @Html.ActionLink("Edit", "Edit", new { id = (int)item.Id }) 
                }
                </text>
        )
    )
)

很明显,您拥有隐藏链接并不能免除您在给定个人ID的情况下对Edit控制器操作中的Age进行相同检查的负担.没有什么可以阻止用户直接在其浏览器地址栏中输入此Edit操作的网址.

Obviously the fact that you have hidden the link doesn't relieve you from the burden to perform the same check against the Age inside the Edit controller action given the person id. There's nothing preventing a user from entering the url of this Edit action directly in his browser address bar.

这篇关于隐藏网络网格列“编辑"; MVC3中基于条件的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆