It is a couple days since I am trying to find what is the problem with my code. I need to create such dynamic links in my project: ParentCategory (Category) - ChildCategory (SubCategory) - Pages (Pages List) - PageDetails (page details by id).
I created ParentCategory and ChildCategory list successfully, both are dynamic, but when I press to go to Pages, this error appears:
The parameters dictionary contains a null entry for parameter 'Id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult GetPages(Int32)' in 'bandymas.Controllers.PageController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
My PageViewModels looks like this:
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; }
}
Here is my PageRepository
public IList<ParentCategory> GetParentCategories()
{
retu _context.ParentCategories.ToList();
}
public IList<ChildCategory> GetChildCategories(int Id)
{
retu _context.ChildCategories.Where(category => category.ParentCategoryId == Id).ToList();
}
public IList<Page> GetPages(int Id)
{
retu _context.Pages.Where(x => x.ChildCategoryId == Id).ToList();
}
And PageController
public ActionResult Index()
{
var pcategories = _pageRepository.GetParentCategories();
retu View(pcategories);
}
public ActionResult GetChildCategories(int ParentCategoryId)
{
var ccategories = _pageRepository.GetChildCategories(ParentCategoryId);
retu 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;
}
retu View(pages);
}
Here is 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>
And Here Is 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>
}
This optional parameter error appears when I go to GetPages View from GetChildCategories View. I tried to change optional parameter in RouteConfig.cs and add int? next to Id,but it is still not working...
