可选参数必须是引用类型

时间:2016-06-01 09:34:33

标签: asp.net-mvc

我试图找到代码的问题已经过了几天。我需要在我的项目中创建这样的动态链接:ParentCategory(Category) - ChildCategory(SubCategory) - Pages(Pages List) - PageDetails(页面详细信息由id)。

我成功创建了ParentCategory和ChildCategory列表,两者都是动态的,但是当我按下转到Pages时,会出现此错误:

  

参数字典包含参数“Id”的空条目   方法的非可空类型'System.Int32'   'System.Web.Mvc.ActionResult GetPages(Int32)'中   'bandymas.Controllers.PageController'。可选参数必须是a   引用类型,可空类型,或声明为可选   参数。参数名称:参数

我的PageViewModels看起来像这样:

 public class Page
    {
        public int Id { get; set; }
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }
        [Required]
        [Display(Name = "ShortDescription")]
        public string ShortDescription { get; set; }
        [Required]
        [Display(Name = "Body")]
        public string Body { get; set; }
        [Required]
        [Display(Name = "Meta")]
        public string Meta { get; set; }
        [Required]
        [Display(Name = "UrlSeo")]
        public string UrlSeo { get; set; }
        [Required]
        [Display(Name = "User Image")]
        public string UserImage { get; set; }
        [Display(Name = "Experience")]
        public int Exp { get; set; }
        [Display(Name = "Logo")]
        public string Logo { get; set; }
        [Display(Name = "Website URL")]
        public string WebURL { get; set; }
        [Display(Name = "Facebook URL")]
        public string FacebookUrl { get; set; }
        [Display(Name = "Address")]
        public string Address { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }
        public string City { get; set; }
        public bool Published { get; set; }
        [DefaultValue(0)]
        public DateTime PostedOn { get; set; }
        public DateTime Modified { get; set; }
        public int ChildCategoryId { get; set; }



    }

    public class ParentCategory
    {

        public int Id { get; set; }
        [Required]
        [Display(Name = "Parent Category Name")]
        public string PCatName { get; set; }
        [Required]
        [Display(Name = "UrlSeo")]
        public string UrlSeo { get; set; }
        [Required]
        [Display(Name = "Description")]
        public string Description { get; set; }

        public ICollection<ChildCategory> ChildCategories { get; set; }
    }

    public class ChildCategory
    {

        public int Id { get; set; }
        [Required]
        [Display(Name = "Parent Category Name")]
        public string CCatName { get; set; }
        [Required]
        [Display(Name = "UrlSeo")]
        public string UrlSeo { get; set; }
        [Required]
        [Display(Name = "Description")]
        public string Description { get; set; }

        public int ParentCategoryId { get; set; }


        public ICollection<Page> Pages { get; set; }
    }

这是我的PageRepository

public IList<ParentCategory> GetParentCategories()
    {
        return _context.ParentCategories.ToList();
    }

    public IList<ChildCategory> GetChildCategories(int Id)
    {
        return _context.ChildCategories.Where(category => category.ParentCategoryId == Id).ToList();
    }

    public IList<Page> GetPages(int Id)

    {
        return _context.Pages.Where(x => x.ChildCategoryId == Id).ToList();
    }

和PageController

public ActionResult Index()
    {
        var pcategories = _pageRepository.GetParentCategories();
        return View(pcategories);
    }

    public ActionResult GetChildCategories(int ParentCategoryId)
    {
        var ccategories = _pageRepository.GetChildCategories(ParentCategoryId);
        return View(ccategories);
    }

    public ActionResult GetPages(int Id)
    {

        var pages = _pageRepository.GetPages(Id);
        foreach (var page in pages)
        {


            page.ChildCategoryId = Id;
            page.UserName = page.UserName;
            page.City = page.UserImage;
            page.Address = page.City;
            page.PhoneNumber = page.Body;
        }
            return View(pages);
    }

这是GetChildCategories View

@using bandymas.Models
@using bandymas.Controllers

<div style="font-family:Arial">
@{
    ViewBag.Title = "GetChildCategories";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>GetChildCategories</h2>
<h2>Child Categories List</h2>
<ul>
    @foreach (PageViewModels.ChildCategory ccategory in @Model)
    {
        <li>
           @Html.ActionLink(ccategory.CCatName, "GetPages", "Page",
new { ChildCategoryId = ccategory.Id }, null)
        </li>
    }
</ul>
</div>

这是GetPagesView

@using bandymas.Models

@using bandymas.Controllers

@{
    ViewBag.Title = "GetPages";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@*@Html.BeginForm("GetPages", "Page",new { Id = Model.Id }, FormMethod.Get, null)*@
@foreach (var page in Model)
{



<div style="font-family:Arial">
    <h2>GetPages</h2>

    <h2>Pages List</h2>
    <ul>
        <li><input type="hidden" name="@page.Id" value="@page.Id" /></li>
        <li>
            @page.UserName
        </li>
        <li>
            @page.City
        </li>
        <li>
            @page.Address
        </li>
        <li>
            @page.PhoneNumber
        </li>
        <li>@Html.ActionLink("Back to ChildCategories List", "GetChildCategories", new { departmentId = @Model.DepartmentId })  </li>
    </ul>
</div>

}

当我从GetChildCategories视图转到GetPages视图时,会出现此可选参数错误。我试图改变RouteConfig.cs中的可选参数并添加int?在Id旁边,但它仍然无效...

1 个答案:

答案 0 :(得分:0)

您的PageController的ActionResult参数名称与您在操作链接中指定的名称相匹配,即

@ Html.ActionLink(&#34;回到ChildCategories列表&#34;,&#34; GetChildCategories&#34;,新{ departmentId = @ Model.DepartmentId})这是GetPagesView

GetChildCategoriesView中的

@ Html.ActionLink(ccategory.CCatName,&#34; GetPages&#34;,&#34; Page&#34;,new { ChildCategoryId = ccategory.Id},null)

由于参数名称不匹配,您收到了该错误。该名称与您在Actionlink中的名称相同。您的页面控制器就像这样......

的PageController:

public ActionResult GetChildCategories(int departmentId)
{
    var ccategories = _pageRepository.GetChildCategories(departmentId);
    return View(ccategories);
}


public ActionResult GetPages(int childCategoryId)
{

    var pages = _pageRepository.GetPages(childCategoryId);
    foreach (var page in pages)
    {


        page.ChildCategoryId = childCategoryId;
        page.UserName = page.UserName;
        page.City = page.UserImage;
        page.Address = page.City;
        page.PhoneNumber = page.Body;
    }
        return View(pages);
}
相关问题