Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > BOOK: Professional ASP.NET MVC 5
|
BOOK: Professional ASP.NET MVC 5
This is the forum to discuss the Wrox book Professional ASP.NET MVC 5 by Jon Galloway, Brad Wilson, K. Scott Allen, David Matson; ISBN: 978-1-118-79475-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2014, 03:54 PM
Authorized User
 
Join Date: Nov 2007
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default Chpt 4 -- MVC 5 EF Code First Database Generation

I created two classes in the Models folder, one for Products and one for Product Groups, modeled exactly on Albums and Genres in the book, where Products contains a Product Group element and both Products and Product Groups are identified by an "Id" element. I then generated the database using the "MVC Controller with Views, Using EF" option, using "Products" as the Model class and creating a data context class as described in the book.

This worked as predicted and expected on the database side. A database is created with the two tables linked properly. One can see and use it in the "Data Connections" server explorer panel. But the controller generated for products, and the corresponding views, do not generate linked ability to view or edit the product group for the products.

Here are my two classes, both separate classes in the "Models" folder:

Code:
	public class Product
	{
		public virtual int ProductId { get; set; }
		public virtual string ProductName { get; set; }
		public virtual ProductGroup ProductGroup { get; set; }
	}
Code:
	public class ProductGroup
	{
		public virtual int ProductGroupId { get; set; }
		public virtual string GroupName { get; set; }
		public virtual List<Product> Products { get; set; }
	}
Here is the properly generated db cs (which, by the way, only added the product group line after creating controllers for both products and product groups):

Code:
    public class MVCTestModelsContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, please use data migrations.
        // For more information refer to the documentation:
        // http://msdn.microsoft.com/en-us/data/jj591621.aspx
    
        public MVCTestModelsContext() : base("name=MVCTestModelsContext")
        {
        }

		public System.Data.Entity.DbSet<MVCTestModels.Models.Product> Products { get; set; }

		public System.Data.Entity.DbSet<MVCTestModels.Models.ProductGroup> ProductGroups { get; set; }
    
    }
But the code does not generate linked views. Here, for example, is the relevant portion of the "Index" view generated for the "Products" controller:

Code:
    <div class="form-horizontal">
        <h4>Product</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.ProductId)

        <div class="form-group">
            @Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
Note that there is no column for product group. Similarly, in the edit view, no drop down list for product group is created.

What may be causing this? I tried to follow the instructions on generating the database and corresponding controllers and views to the T.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chpt 12 - Error opening database in VS2012 stevemagid BOOK: Beginning ASP.NET 4 : in C# and VB 2 September 24th, 2012 02:40 PM
Chpt 12: Including MVC in Existing Web Form Apps Yields no Results! jlakes BOOK: Professional ASP.NET MVC 2 4 November 30th, 2010 03:30 PM
Chpt 3 - Connect to SQL Server 2000 Database BennyHill BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 6 June 7th, 2007 03:30 PM
Order of Code Generation Problem forfree ASP.NET 1.0 and 1.1 Basics 1 July 5th, 2006 09:48 PM
JSP Code Generation using XML ? javadude XSLT 0 June 9th, 2005 07:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.