【发布时间】:2016-06-01 18:28:00
【问题描述】:
自从我试图找出我的代码有什么问题以来已经有几天了。我需要在我的项目中创建这样的动态链接:ParentCategory (Category) - ChildCategory (SubCategory) - Pages (Pages List) - PageDetails (page details by id)。
我成功创建了ParentCategory和ChildCategory列表,都是动态的,但是当我按下去Pages时,出现这个错误:
参数字典包含参数“Id”的空条目 方法的不可空类型“System.Int32” 'System.Web.Mvc.ActionResult GetPages(Int32)' 在 'bandymas.Controllers.PageController'。可选参数必须是 引用类型,可为空的类型,或被声明为可选 范围。参数名称:参数
我的 PageViewModel 如下所示:
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();
}
还有页面控制器
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 视图
@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旁边,但它仍然无法正常工作......
【问题讨论】:
-
也许您可以将
GetPages(int Id)更改为GetPages(int? Id)?如果您的视图发送null,那么操作方法应该接受nullable参数,而不是valuetype,因为int确实不能是null。所以改用 nullable int (int?)
标签: asp.net-mvc