I am curious on how does EF work.
I have the following code. I want it to
1) display my category in the home page.
2) And when user chosen a category, it should display the sub category.
3) And when sub category chosen, it should display the list of books in that sub category.
Problem encountered:
I am able to get to step 2. So, following the same idea, i created a method, 'BookPDF', hoping to to display the list of books. However, this is not successful.
From the URL,
http://localhost:1090/Home/BookPDF?subid=1002
I can tell that the subid is passed from previous view (SubCategory) but no book title displayed.
Please find the print screen for the steps that i had gone through and the database query result in the attachment. This is to show that i have data populated, but the EF refused? to pick up the book title.
Can someone please help me with my problem?
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCLibrary.Models;
namespace MVCLibrary.Controllers
{
public class HomeController : Controller
{
FYP_OPL_FileStreamEntities db = new FYP_OPL_FileStreamEntities();
public ActionResult Index()
{
//return View();
var categories = db.Categories.ToList();
return View(categories);
}
public ActionResult SubCategory(int? s_catid)
{
if (s_catid != null)
{
var subcategories = db.Categories.Include("Sub_Categories").Single(s => s.C_Catid == s_catid);
return View(subcategories);
}
else
return View();
}
public ActionResult BookPDF(int? s_subId)
{
if (s_subId != null)
{
var bookdetails = db.BookDetails.Include("BookPDF").Single(b => b.B_SubId == s_subId);
return View(bookdetails);
}
else
//return RedirectToAction("SubCategory");
return View();
}
[/code]
and my Index
[code]
@model IEnumerable
@{
ViewBag.Title = "Home Page";
}
- @Html.ActionLink(Categories.C_Description, "SubCategory", new { s_catid = Categories.C_Catid })
@foreach (var Categories in Model)
{
}
[/code]
and my SubCategory
[code]
@model MVCLibrary.Models.Category
@{
ViewBag.Title = "SubCategory";
}
Browsing SubCategory
@Html.ActionLink(subcategory.S_Description, "BookPDF", new { subid = subcategory.S_SubId })
@foreach (var subcategory in Model.Sub_Categories)
{
}
[/code]
and my BookPDF
[code]
@model IEnumerable
@{
ViewBag.Title = "BookPDF";
}
BookPDF
@if(Model != null )
{
- @Html.ActionLink(books.B_Title, "BookPDF", new { B_ISBN = books.B_ISBN })
@foreach (var books in Model)
{
}
}
Record not found
[/code]
TK LimPosted Nov 14, 2014, 9:01 AM
I found solution to my problem. In this MVC, there is a standard way (by using ActionLink) of passing value from View to Action Model.
The tutorial i followed used the following:
new { user_defined_variable = Model.column_name }
The standard way:
new { id = Model.column_name }
So, after i changed my code to use the word ' id ', it then just magically works.
Thanks for helping or trying to help.
TK LimPosted Nov 7, 2014, 4:21 AM
Edit 2014 11 08
Today i did the following modification to try to understand how does MVC pass value. After the changes, it now return my Book Title correctly. I notice the differences as following:
The index View is referencing the Category model with the keyword "IEnumerable".
The SubCategory View does not has that.
And so, i tried to put that keyword into the SubCategory View and it now gave me the following error message:
CS1061: 'System.Collections.Generic.IEnumerable
So, is that keyword that caused the problem? What type of value is it returning? Can i type cast it to something that the model can understand?
//index (View)
@model IEnumerable<MVCLIbraryv1.Models.Category>
@{
ViewBag.Title = "Home Page";
}
@foreach (var Categories in Model)
{
}
//SubCategory (Model)
public ActionResult SubCategory(int? s_catid)
{
if (s_catid != null)
{
var subcategories = db.Categories.Include("BookDetails").Single(c => c.C_Catid == s_catid);
return View(subcategories);
}
else
return View();
}
//SubCategory (View)
@model MVCLIbraryv1.Models.Category
@{
ViewBag.Title = "BookDetail";
}
Browsing BookDetail
@if (Model != null)
{
@foreach (var subcategory in Model.BookDetails)
{
}
}
Edit 2014 11 12
Previously i thought it was the 'IEnumerable' problem. But after i made the following changes, and i am able to run the program, it then proof that 'IEnumerable' is not the problem. And yet, the BookPDF still not getting the value passed from the view.
Modification to model SubCategory:
Browsing SubCategory
Jaganathan BantheswaranPosted Nov 5, 2014, 7:16 AM
Try to pass null as the fourth parameter.
@Html.ActionLink(subcategory.S_Description, "BookPDF", new { subid = subcategory.S_SubId }, null)
TK LimPosted Nov 5, 2014, 6:58 AM
Thanks for the reply.
Yes, I did debug. But I don't understand.
If I put break point on that line, nothing will happen because it will not get called. For unknown reason, s_subId is equal to null. So, my checking of
if (s_subId != null) ...else return View();
will direct the program to run the next line instead of the line i put break point on.
But, if you look at my attached print screen, you will find that there is a value '1002' in the URL, which is passed from view (SubCategory).
I did also include a print screen of my database query result in the same attachment, which shows that i have data in the database. I just don't understand where did i do wrong so that EF do not pick up the book title from database.
Regards
Jaganathan BantheswaranPosted Nov 5, 2014, 6:28 AM
var bookdetails = db.BookDetails.Include("BookPDF").Single(b => b.B_SubId == s_subId);
Does bookdetails has anything?? Did you the collection BookDetails??