如何在CSHTML视图中使用的WebGrid?(How to use WebGrid in a cs

2019-06-25 11:59发布

我能够使用的WebGrid像任何控制器:

var grid = new WebGrid(emailsFetched, columnNames);

我必须在我的ASP.NET MVC项目添加到一个参考System.Web.Helpers这一点。

但是,当我尝试使用鉴于此Web电网直接(以避免在控制器实例化和其他设置),它说: The type or namespace 'WebGrid' cannot be found 。 好吧,我想在这里添加引用过:

@using System.Web.Helpers但是,这将引发另一个问题:

There is no build provider registered for the extension '.cshtml'. You can register one in the <compilation><buildProviders> section in the machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.

这是非常奇怪的。我已经看到了网上被使用的WebGrid并没有宣布在CSHTML视图什么足够的例子...

你能告诉我怎么解决这个问题? 为什么我会遇到这样的非常难看的问题?

Answer 1:

最后,我已经能够注意到这一点:

<assemblies> <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies>

已经在web配置中添加,下system.web节,withing compilation代码,因此它看起来就像:

<system.web>
    <compilation debug="true" targetFramework="4.0">
        <assemblies>
            <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </assemblies>
    </compilation>
</system.web>


Answer 2:

尝试按照下面的步骤之前已经在测试ASP.NET MVC4项目没有任何问题。

1)打开NuGet Package ManagerVisual Studio ,然后搜索“微软网络助手”安装。

2)安装它后打开web.config在解决方案中,改变connectionStringName参数DefaultMembershipProviderDefaultRoleProvider已经DefaultSessionProvider (如果你不这样做,你可能会遇到“DefaultConnection”在应用程序的配置没有被发现或连接字符串是空的。”错误。

3)重建项目,然后在你的Razor视图使用smilar定义如下图所示。

注:更改“标题”,“ 控制器”“行动”的名字Html.ActionLinks根据您的项目。

视图:

@{
var grid = new System.Web.Helpers.WebGrid(
    source: Model,
    columnNames: new List<string>() { "Title" },
    ajaxUpdateContainerId: "myGrid",
    defaultSort: "Name",
    canPage: true,
    canSort: true,
    rowsPerPage: 5
    );
grid.SortDirection = SortDirection.Ascending;
}


@grid.GetHtml(
      tableStyle: "table", /*your class name for this property*/
      headerStyle: "webgrid-header",/*your class name for this property*/
      footerStyle: "webgrid-footer", /*your class name for this property*/
      rowStyle: "webgrid-row-style", /*your class name for this property*/
      alternatingRowStyle: "webgrid-alternating-row",/*your class name...*/                                         selectedRowStyle: "webgrid-selected-row",/*your class name for this property*/

      firstText: "<<",
      lastText: ">>",
      mode: WebGridPagerModes.All,
      fillEmptyRows: true,

      columns: grid.Columns(
       grid.Column("ApplicantID", "No", style: "span1", canSort: true),
       grid.Column("Name", "Name", style: "span2", canSort: true),
       grid.Column("Surname", "Surname", style: "span2", canSort: true),
       grid.Column("Organization", "Org.", style: "span2", canSort: true),
       grid.Column("MeetingId", "Meeting", style: "span1", canSort: true),
        //some format usage samples:
        //grid.Column("Email", "e-mail", style: "span1", canSort: true, format: @<a href="mailto:@item.Email">@item.Email</a>),  
        //grid.Column("BirthDate", format: p=>p.BirthDate.ToShortDateString()),

//for using multiple Html.ActionLink in a column using Webgrid
grid.Column("Operations", format: (item) =>
 new HtmlString(
       Html.ActionLink("Show Details", "Edit", "Admin", new
       {
           applicantId = item.ApplicantID,               
           title = "Detail",
           @class = "icon-link",
           style = "background-image: url('../../Content/icons/detail.png')"
       }, null).ToString() +
       Html.ActionLink("Edit Record", "Edit", "Admin", new
       {
           applicantId = item.ApplicantID, 
           title = "Edit",
           @class = "icon-link",
           style = "background-image: url('../../Content/icons/edit.png')"
       }, null).ToString() +
       Html.ActionLink("Delete Record", "Edit", "Admin", new
       {
           applicantId = item.ApplicantID,
           title = "Delete",
           @class = "icon-link",
           style = "background-image: url('../../Content/icons/delete.png')"
       }, null).ToString()
 )
)
),
numericLinksCount: 5
)


下面是css低于使用的类Razor 。 如果你想用你的css定义,简单地改变样式属性到你的(有些属性是可选的,因为在的那些Razor View )。

<style type="text/css">    
    .webgrid-operations { /*for limiting the width of Operations 
                          menu used in the WebGrid*/
    width: 65px;
}

.webgrid-header td {
    text-align: left;
}

.webgrid-header th {
    background-color: #EFEFEF;
    margin-bottom: 2px;
}

.webgrid td {
    padding-right: 15px;
}

.webgrid-footer td {
    font-family: 'open_sanssemibold', sans-serif;
    font-size: 1em;
    text-align: right !important;
    padding-right: 21px !important;
    color: #000;
    background-color: #EFEFEF;
}

    .webgrid-footer td a {
        text-align: right !important;
        padding: 0 .4em 0 .4em;
        font-size: .83em;
        text-decoration: none;
        color: #FFFFFF;
        border: 1px solid #C0C0C0;
        background-color: #808080;
    }

        .webgrid-footer td a:hover {
            background-color: #6BBEC7;
        }

        .webgrid-footer td a.selected {
            background-color: #f00;
            color: #f00;
        }

.webgrid a {
    color: #fff;
}

.colRowButton {
    width: 70px;
    text-align: left;
}

.webgrid-row-style {
    /*border-bottom: 1px solid #E8EEF4;*/
}

.webgrid-alternating-row td {
    /*background-color: #f9f9f9;*/
}

.webgrid-selected-row {
    /*font-weight: bold;*/
}    

<style type="text/css">
    a.icon-link {
        background-color: transparent; 
        background-repeat: no-repeat; 
        background-position: 0px 0px;
        border: none;
        cursor: pointer; 
        width: 16px;
        height: 16px;
        margin-right: 8px; 
        vertical-align: middle;
    }

    .span5 {
    width:380px
    }
    .span4 {
    width:300px
    }
    .span3 {
    width:220px
    }
    .span2 {
    width:140px
    }
    .span1 {
    width:60px
    }
    </style>
}


希望这可以帮助...



Answer 3:

就遇到了这个问题。 真的不能把功劳,但我卸载早期版本,并从重新安装的NuGet最新版本的Microsoft ASP.NET MVC4和事情为我工作。 希望这可以帮助别人。 尝试了所有的解决方案,但是这只是工作的事。 http://forums.asp.net/t/1823940.aspx?MVC4+WebGrid+problem+in+View+Razor+



文章来源: How to use WebGrid in a cshtml view?