使用MVC网格组件组件的参数无效?

时间:2014-03-31 19:32:45

标签: .net asp.net-mvc-4 view entity-framework-4 mvcgrid

我是MVC应用程序开发的新手,正在尝试MVC Grid组件。如链接所示,我有用户模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using PROJECT.Helper;

namespace PROJECT.Models
{
    public class Users
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Display(Name = "Name")]
        public string Name { get; set; }

        [Display(Name = "Forum Username")]
        public string ForumUsername { get; set; }

        [Display(Name = "Last Date Visited")]
        public DateTime LastVisitDate { get; set; }

        public Boolean RememberMe { get; set; }

        public int? OrganizationId { get; set; }

        [Display(Name="Position")]
        public string Position { get; set; }

        [Display(Name = "Profile Picture")]
        public string ProfilePictureSrc { get; set; }

        //[DataType(DataType.PhoneNumber)]
        [DisplayFormat(DataFormatString = "{0:(###) ###-####}", ApplyFormatInEditMode = true)]
        public string PhoneNumber { get; set; }

        public virtual string ProfilePictureUrl
        {
            get
            {

                //TODO: update the folder when it isn't null to the blob location
                if (this.ProfilePictureSrc == null)
                {
                    return "/Content/Images/userThumb.png";
                }
                else
                {
                    BlobHelper helper = new BlobHelper();
                    string url = helper.getImageUrl(this.ProfilePictureSrc, "profilepicture");

                    return url;
                }
            }
        }
    }
}

接下来我有控制器(〜/ Controllers / AdminController.cs):

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PROJECT.Models;
using PROJECT.DAL;
using System.IO;
using PROJECT.Helper;
using PagedList;

namespace PRISM.Controllers
{
    [CustomizedAuthorizeAttribute]
    public class AdminController : Controller
    {
        private ProjectContext db = new ProjectContext();
        private static string profPicBlobContainer = "ProfilePicture";
        private BlobHelper blobHelper = new BlobHelper();

        //
        // GET: /Admin/
        public ActionResult ViewUsers()
        {
            return View(db.Users.ToList());
        }
    }
}

然而,当我加载( localhost:12345 / Admin / ViewUsers )时,我收到:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0411: The type arguments for method 'GridMvc.Html.GridExtensions.Grid<T>(System.Web.Mvc.HtmlHelper, System.Collections.Generic.IEnumerable<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.


Line 9:  @using GridMvc.Html
Line 10: 
Line 11: @Html.Grid(Model).Columns(columns =>
Line 12:            {
Line 13:                  //columns.Add().Titled("Custom column title").SetWidth(110);


Source File: c:\ME\Project\Project\Views\Admin\ViewUsers.cshtml    Line: 11 

〜/查看/管理/ ViewUsers.cs:

@using GridMvc.Html
@model PROJECT.Models.Users

@{
    ViewBag.Title = "View Users";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}

@using GridMvc.Html

@Html.Grid(Model).Columns(columns =>
           {
                 //columns.Add().Titled("Custom column title").SetWidth(110);
                 columns.Add(Model.Name).Sortable(true);
           }).WithPaging(20)

我可以告诉我,我没有向MVC网格提供正确的“模型”集合,以便它可以访问单个模型属性等。

任何人都有我如何解决这个问题的想法?链接的例子是迄今为止我唯一能找到的例子。


修改

我通过在顶部将它指定为IEnum(如下所示)来获取要渲染的视图。

现在,在我的第一个@Html.Grid(Model)部分中,我可以声明要引入的特定列,以及设置各自的网格属性。在第二部分@Html.Grid(Model).AutoGenerateColumns()中,我使用GridColumn Annotation来指定属性,然后AutoGenerateColumns()按照模型中列出的降序顺序将它们拉入。

我遇到AutoGenerateColumns()的问题虽然是我无法弄清楚如何隐藏我不希望显示的特定模型属性。有没有人对此有任何想法?我希望尽可能使用AutoGenerateColumns()来减少编码并能够在我的模型中编辑所有内容。

EDIT2 :使用[NotMappedColumn]网格数据注释找到答案。 Link

@using GridMvc.Html
@model IEnumerable<PROJECT.Models.Users>

@{
    ViewBag.Title = "View Users";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}

@using GridMvc.Html

@Html.Grid(Model).Columns(columns =>
           {
               columns.Add().Encoded(false).Sanitized(false).SetWidth(30).RenderValueAs(o => @Html.CheckBox("checked", false));
               columns.Add(foo => foo.Enabled).Titled("Enabled").SetWidth(50).Sortable(true);
               columns.Add(foo => foo.Name).Titled("Name").SetWidth(100).Sortable(true);
               columns.Add(foo => foo.Position).Titled("Position").SetWidth(50).Sortable(true);
               columns.Add(foo => foo.Email).Titled("Email").SetWidth(100).Sortable(true);
               columns.Add(foo => foo.PhoneNumber).Titled("Phone#").SetWidth(30).Sortable(true);
               columns.Add(foo => foo.ForumUsername).Titled("Forum Username").SetWidth(50).Sortable(true);
               columns.Add(foo => foo.RegisteredDate).Titled("Reg. Date").SetWidth(50).Sortable(true);
               columns.Add(foo => foo.LastVisitDate).Titled("Last Visited").SetWidth(50).Sortable(true);
               columns.Add(foo => foo.ReceiveSystemEmails).Titled("Rec. Sys Emails").SetWidth(50).Sortable(true);
           }).WithPaging(5)

@Html.Grid(Model).AutoGenerateColumns()

〜/型号/用户:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using PROJECT.Helper;
using GridMvc.DataAnnotations;

namespace PROJECT.Models
{
    [GridTable(PagingEnabled = true, PageSize = 5)]
    public class Users
    {
        public int Id { get; set; }

        [Display(Name = "Enable User")]
        [GridColumn(Title = "Enabled", SortEnabled = true, FilterEnabled = true, Width = "30")]
        public bool Enabled { get; set; }

        [Display(Name = "Name")]
        [GridColumn(Title = "Name", SortEnabled = true, FilterEnabled = true, Width = "50")]
        public string Name { get; set; }

        [Display(Name = "Position")]
        [GridColumn(Title = "Position", SortEnabled = true, FilterEnabled = true, Width = "50")]
        public string Position { get; set; }

        [Required]
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        [GridColumn(Title = "Email", SortEnabled = true, FilterEnabled = true, Width = "80")]
        public string Email { get; set; }

        //[DataType(DataType.PhoneNumber)]
        [DisplayFormat(DataFormatString = "{0:(###) ###-####}", ApplyFormatInEditMode = true)]
        [GridColumn(Title = "Phone #", SortEnabled = true, FilterEnabled = true, Width = "40", Format = "{0:(###)-###-####}")]
        public string PhoneNumber { get; set; }

        [Display(Name = "Forum Username")]
        [GridColumn(Title = "Forum Username", SortEnabled = true, FilterEnabled = true, Width = "30")]
        public string ForumUsername { get; set; }

        [GridColumn(Title = "Reg. Date", SortEnabled = true, FilterEnabled = true, Width = "40")]
        public DateTime RegisteredDate { get; set; }

        [Display(Name = "Last Date Visited")]
        [GridColumn(Title = "Last Visited", SortEnabled = true, FilterEnabled = true, Width = "40")]
        public DateTime LastVisitDate { get; set; }

        [Display(Name = "Receive System Emails")]
        [GridColumn(Title = "Rec. Sys Emails", SortEnabled = true, FilterEnabled = true, Width = "30")]
        public bool ReceiveSystemEmails { get; set; }

        [GridColumn(Title = "Remember User", SortEnabled = true, FilterEnabled = true, Width = "20")]
        public Boolean RememberMe { get; set; }

        [Required]
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        public int? OrganizationId { get; set; }

        [Display(Name = "Profile Picture")]
        [GridColumn(Title = "Profile Pic.", SortEnabled = true, FilterEnabled = true)]
        public string ProfilePictureSrc { get; set; }

        [ForeignKey("OrganizationId")]
        public virtual MemberOrganizations Organization { get; set; }

        public virtual SponsorOrganizations Sponsor { get; set; }
        public virtual ICollection<UserRoles> Roles { get; set; }

        public virtual string ProfilePictureUrl
        {
            get
            {

                //TODO: update the folder when it isn't null to the blob location
                if (this.ProfilePictureSrc == null)
                {
                    return "/Content/Images/userThumb.png";
                }
                else
                {
                    BlobHelper helper = new BlobHelper();
                    string url = helper.getImageUrl(this.ProfilePictureSrc, "profilepicture");

                    return url;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您只将User的一个实例传递给Html.Grid()。 Html.Grid()期望一个继承自IEnumerable的参数。

此外,应该将User类称为User,因为它只代表一个用户。

以下是您要做的事情的示例:

@using GridMvc.Html
@model List<PROJECT.Models.User>

@{
    ViewBag.Title = "View Users";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}

@Html.Grid(Model).Columns(columns =>
{
    columns.Add(Model.Name).Sortable(true);
}).WithPaging(20)