5
Reply

MVC, value passed from view to controller, EF sleeping?

TK Lim

TK Lim

Nov 4 2014 7:37 PM
1.1k
Dear All,


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<MVCLibrary.Models.Category>
@{
    ViewBag.Title = "Home Page";
}


<ul>
    @foreach (var Categories in Model)
    {
        <li>@Html.ActionLink(Categories.C_Description, "SubCategory", new { s_catid = Categories.C_Catid })</li>
    }
</ul>
[/code]


and my SubCategory
[code]
@model MVCLibrary.Models.Category


@{
    ViewBag.Title = "SubCategory";
}


<h2>Browsing SubCategory</h2>


<ul>
    @foreach (var subcategory in Model.Sub_Categories)
    {
        <li>
            @Html.ActionLink(subcategory.S_Description, "BookPDF", new { subid = subcategory.S_SubId })
        </li>
    }
</ul>
[/code]


and my BookPDF
[code]
@model IEnumerable<MVCLibrary.Models.BookDetail>


@{
    ViewBag.Title = "BookPDF";
}


<h2>BookPDF</h2>


@if(Model != null )
{
<ul>
    @foreach (var books in Model)
    {
        <li>@Html.ActionLink(books.B_Title, "BookPDF", new { B_ISBN = books.B_ISBN })</li>
    }


</ul>
}


<div>Record not found</div>
[/code]




Answers (5)