Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC 4 WebGrid EF_Asp.net_Asp.net Mvc_Entity Framework_Webgrid - Fatal编程技术网

ASP.NET MVC 4 WebGrid EF

ASP.NET MVC 4 WebGrid EF,asp.net,asp.net-mvc,entity-framework,webgrid,Asp.net,Asp.net Mvc,Entity Framework,Webgrid,我有一些关于Webgrid的简短问题 我怎样才能在一列中输入当前的数字?(我正在使用EF) 如何显示字符串而不是整数? (假设我有一个int字段高度,如果它在1.50-1.60之间,我希望在webgrid中看到small,1.60-1.70-正常,1.7-1.8-大,>2-大) 阴极射线管。编号|旧高度|新高度 1 | 1.55 |小 2 | 1.78 |正常 3 | 2.40 |巨大 @grid.GetHtml(tableStyle: "table", alternatin

我有一些关于Webgrid的简短问题

  • 我怎样才能在一列中输入当前的数字?(我正在使用EF)

  • 如何显示字符串而不是整数? (假设我有一个int字段高度,如果它在1.50-1.60之间,我希望在webgrid中看到small,1.60-1.70-正常,1.7-1.8-大,>2-大)

  • 阴极射线管。编号|旧高度|新高度

    1 | 1.55 |小

    2 | 1.78 |正常

    3 | 2.40 |巨大

    @grid.GetHtml(tableStyle: "table",
              alternatingRowStyle: "alternate",
              headerStyle: "header",
              columns: grid.Columns(
              grid.Column(columnName: "?",header: "Crt. No.", canSort: true),
              grid.Column(columnName: "height", header: "height old", canSort: true),
              grid.Column(columnName: "height?", header: "height new", canSort: true)))
    
    1) 。我怎样才能在一列中输入当前的数字?(我正在使用EF)

    您可以使用视图模型,而不是将WebGrid绑定到
    模型
    ,而是将其绑定到
    模型。选择((item,index)=>new{index=index,Element=item})
    ,或者更好地使用具有这两个属性的真实视图模型,而不是使用匿名对象

    2) 。如何显示字符串而不是整数?(假设我 有一个int字段高度,如果它在1.50-1.60之间,我想 请参见webgrid中的“小”,1.60-1.70-正常,1.7-1.8-大,>2- (巨大的)

    您可以对列使用自定义的
    格式

    下面是一个例子:

    @model IEnumerable<SomeModel>
    
    @{
        var grid = new WebGrid(Model.Select((item, index) => new { Index = index, Element = item }));
    }
    
    @grid.GetHtml(
        tableStyle: "table",
        alternatingRowStyle: "alternate",
        headerStyle: "header",
        columns: grid.Columns(
            grid.Column(columnName: "Index", header: "Crt. No.", canSort: true),
            grid.Column(
                header: "height old", 
                canSort: true, 
                format: 
                    @<text>
                        @item.Element.height
                    </text>
            ),
            grid.Column(
                header: "height new", 
                canSort: true, 
                format: 
                    @<text>
                        @Html.FormatHeight((double)item.Element.height)
                    </text>
            )
        )
    )
    

    效果很好。还有两件事:当前数字是否可以以1而不是0开头?为什么即使grid.Column(…,canSort:true)WebGrid(Model.Select((item,index)=>new{index=index+1,…}add columnName:grid.Column(columnName:“Element.”我也不能再排序了。我怎么能从数据库中简单地添加两个数字并在一列中显示它们呢?漫长的道路:公共静态IHtmlString AddNO(int no1,int no2){返回no1+no2;}?
    public static class HtmlExtensions
    {
        public static IHtmlString FormatHeight(this HtmlHelper htmlHelper, double height)
        {
            if (height < 1.5)
            {
                return new HtmlString("tiny");
            }
            if (height > 1.5 && height < 1.6)
            {
                return new HtmlString("small");
            }
            else if (height > 1.6 && height < 1.7)
            {
                return new HtmlString("normal");
            }
            else if (height > 1.7 && height < 1.8)
            {
                return new HtmlString("big");
            }
    
            return new HtmlString("huge");
        }
    }