Reputation: 99
first of all im using entity framework and mvc5 I have a category table and a subcategory table, i made a foreign key of the categoryid in the subcategory table, im trying to wirte a query that compares the categoryid in the subcategory table with the categoryid in categories table and put it into code that when i click on a category i get the subcategories that have the same categoryid of it.
Here are the tables first:
public class Category
{
public Category()
{
SubCats = new HashSet<SubCat>();
}
public int Id { get; set; }
public string Description { get; set; }
[ForeignKey("SubCats")]
public int? SubCatId { get; set; }
public ICollection<SubCat> SubCats { get; set; }
}
public class SubCat
{
public int Id { get; set; }
public string Description { get; set; }
[ForeignKey("Category")]
public int CategoryId { get; set; }
public Category Category { get; set; }
}
This is the code i wrote in my categories controller:
public class CategoriesController : Controller
{
new ApplicationDbContext _context;
public CategoriesController()
{
_context = new ApplicationDbContext();
}
// GET: Categories
public ActionResult Index()
{
var category = _context.Categories.ToList();
var cat = new CategoryViewModel
{
Categories = category
};
return View(cat);
}
public ActionResult Query()
{
var category = _context.Categories.ToList();
var subcat = _context.SubCats.ToList();
var cats = new CatSubCatViewModel
{
Categories = category,
SubCats = subcat
};
return View(cats);
}
}
I think i should place the query actionresult in the subcat controller!
this is the categories index view:
@model Ebart.ViewModels.CategoryViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Categories</h1>
<br/>
<ul>
@foreach (var category in Model.Categories)
{
<a href="http://localhost:51438/Categories/Query"><li>@category.Description</li></a>
}
</ul>
I want when i click on the li here to redirect me to the subcats that has its categoryid
And this is the view of the query:
@model Ebart.ViewModels.CatSubCatViewModel
@{
ViewBag.Title = "Query";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Sub-Categories</h2>
@{
var query =
from a in Model.Categories
from b in Model.SubCats
where b.CategoryId == a.Id
select b;
}
<ul>
@foreach (var c in query.Distinct())
{
<li>@c.Description</li>
}
</ul>
Oh and i made view models that contain lists of categories and subcategories:
public class CategoryViewModel
{
public IEnumerable<Category> Categories { get; set; }
}
public class CatSubCatViewModel
{
public IEnumerable<Category> Categories { get; set; }
public IEnumerable<SubCat> SubCats { get; set; }
}
what is the easiest way to solve it??
Upvotes: 0
Views: 49
Reputation: 47
Try instead <li>
:
@Html.ActionLink(c.Description, "Query", "CategoriesController, new { id= item.id })
U should move query
from View
to Controller
I think.
And set Query
method to:
public ActionResult Query(id)
{
var cat = _context.Categories
.Include(c=>c.Subcat)
.FirstOrDefault(c=>c.Id == id)
return View(cat);
}
Now u can use i your destination view f.e. @Html.DisplayFor(modelItem => item.Description)
Upvotes: 0