p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 (http://p2p.wrox.com/forumdisplay.php?f=472)
-   -   Chapter 5 UserController.cs P.119-129 (http://p2p.wrox.com/showthread.php?t=75635)

ulikkg May 27th, 2010 06:40 PM

Hi fourpastmidnight,

Thanks a lot for explanation! With one word - GREAT! I will try to follow your steps - I hope it solves.

Nick, I understand your case and it is not easy to write book parallel to the release and as I already said I am not against using external libraries. The point is I don't see any explanation neither in the book nor in the forums for the usage of external libraries. Now, you again send me the link to the files. What should I do with these files - not clear. I hope fourpastmidnight's explanation will help me to get out of the problem.

Other question is in the ProfileInformation.cs there are GetCountryList, GetOccupationList and GetLanguageList methods. In them I have error with TheBeerHouseDataContext. When and how did you create this datacontext - I know with LINQ but how exactly? When did you create these tables in DB? I am not going to download and run the whole code - I AM FOLLOWING THE BOOK and SURPRISES. You must understand us as a readers. Can you please clarify these errors in ProfileInformation.cs?

Regards & thanks in advance,

fourpastmidnight May 27th, 2010 07:16 PM

Adding the LINQ to SQL Classes file to the project

I'm currently having a problem with this, too. Let me help you with this, as I solved half of it.

The author told us at the beginning of the book that he would be using Linq-To-SQL. But again, never were there any steps in the book that explained how to do this, nor when. Luckily, I've had exposure to it before.

First off, the database is included with the source code in the App_Data folder in the TheBeerHouse folder that houses all the code for this project.

In your project, if you don't already have an App_Data folder, right-click your project, hover over Add ASP.NET Folder and choose App_Data.

Extract the database from the zipped up code files and place it in the App_Data folder. Then add the database files to your project in the App_Data folder.

Next, right-click your project and choose Add. On the categories pane, Web will most likely be selected. Choose the Data category instead.

On the right side, choose LINQ to SQL Classes. Name the file TheBeerHouse.dbml. Now, I already did this in my project, so I'm trying to recall this from memory. But, a dialog should pop up to get information it needs for making a connection to the database. The first dialog needs to know what the file will be connected to. In your case, I believe it is a SQL Server Database File (again, I'm stating these steps from memory).

I believe at that point, you will be asked to choose a file. Navigate to your App_Data directory and choose the *.mdb file. That should take care of getting the TheBeerHouseDataContext class. However, I don't think this will solve your problem.

I know for me, I need to create the Languages table in the database that was described in the Design section, but not the Solutions section. Also, apparently there's supposed to be an Occupations table that was never discussed in the book. [:(]

Let me know if you figure out anything. [:)]

fourpastmidnight May 27th, 2010 07:38 PM

Getting Chapter 5 to Work
Ok, here's the rest on the LINQ to SQL.

In Visual Studio, go to the View menu and choose Server Explorer.

Expand Data Connections. You should see your database there. In any event, expand your database and then the Tables folder.

Double-click your TheBeerHouse.dbml file to open up the LINQ to SQL designer. Now, back in Server Explorer, drag the Language(s) table and the Occupation(s) table (I'm not sure if they're plural or not) onto the designer surface. The designer will name the corresponding objects Language and Occupation. Rename these objects to Languages and Occuptations, respectively.

You should be good to go.


ulikkg May 28th, 2010 04:47 AM

Hi fourpastmidnight,

Thank you very much. I really appreciate your help and great effort. Currently I am at work I will try evening at home. Yesterday I went to bed :)

Best Regards,

fourpastmidnight May 28th, 2010 05:43 PM

Getting Chapter 5 to Work
Well, I went on to chapter 6 after I figured out how to get Chapter 5 to work, only to find that in chapter 6 do they finally tell you how to set up all the LINQ to SQL stuff.

The editing on this book is terrible. How can you say in Chapter 5, "Well, that's everything you need to get the site working up to this point" when in reality, you need stuff in Chapter 6 to make stuff in Chapter 5 work??

Really, the organization of the content is about the worst I've seen (not _the_ worst, but right there with _the_ worst). This is not a reflection on the author, though he/they have some control over this--but rather, the editor and others involved in pre-production are the ones responsible for making sure that the content is organized properly before the book goes to print.

So if you can't get chapter 5 to work because you're missing classes, see chapter 6. In addition, as I stated before, the classes you will be missing are the Languages class and the Occupations class, which are LINQ to SQL classes from the Languages and Occupations tables in the database. However, chapter 6 does not tell you how to add these to LINQ to SQL, but it's easy enough to do. Just follow the last few steps of my previous post on this thread after having read through chapter 6.

ulikkg May 30th, 2010 08:16 AM

Lots of errors
Hi fourpastmidnight,

Finally I got it run. BUT except these errors there is lot's of missing parts. No mention about View inheritances, Some important actions are not existing/mentioned in book. Role and User deletion is not working still. Everytime I found solutions by comparing book with downloaded files and there are lots of differences. I am absolutely agree with you that they implemented LINQ to SQL from chapter 6 but they are being used in chapter 5 - what a silly thing.

As for database, in addition to other tables I put IsoCodes inside one table - why it is being retrieved from CSV file - not good practice for me.

Last comment about your last statement about role management. In standard MVC solution there is no role management interface - as I know there are only for Sign In, Registration, Forgot Password.

Thanks once again, I hope next chapters are more clear.

fourpastmidnight May 30th, 2010 05:20 PM

It took a lot to fix all the errors

Well, first, I'd like to say that so far, I've managed to completely do away with the ManagedFusion.dll and ManagedFusion.Web.dll.

MVC 2.0 includes new attributes that can be used in place of the ManagedFusion.*.dll. For instance, whenever the HttpPostOnly attribute is used, you can replace it with the MVC 2.0 HttpPost attribute, which does the same thing. Apparently, according to the documentation for it, it's a shorthand for the AcceptVerbsAttribute. Any action decorated with the HttpPostAttribute will only accept requests with the HTTP POST verb. Perhaps the behavior of this has changed since MVC PR, which is what this book was based on (PR = Preview Release, essentially, an alpha version of the 1.0 framework).

Secondly, for the Service and Service only attributes, instead of returning a View( <some data> ), you can instead return a more specfic type of ActionResult, such as Json( <my json data> ). Then you don't need the ServiceAttribute or ServiceOnlyAttribute.

The IsoCodes are located in the source code in the folder App_GlobalResources. This took me a little while to get working. When you import all the files into your project, the project doesn't automatically "hook up" the *.resx code-behind files. I had to manually edit my project file to get everything to work.

Basically, right-click your project and choose Add --> Add ASP.NET Folder and choose App_GlobalResources. Then extract the files from the source code download in the same folder into your project folder. Next, close all files you have open associated with the project. Next, right-click your project and choose Unload Project.

Next, right-click the project and, IIRC, there is' an Edit Project item. In any event, it will open up the XML file that resembles your project. Locate the following XML:


<Content Include="App_GlobalResources\Messages.designer.cs" />
<Content Include="App_GlobalResources\Messages.it-IT.designer.cs" />

Replace it with the following:


<Content Include="App_GlobalResources\Messages.designer.cs">
<Content Include="App_GlobalResources\Messages.it-IT.designer.cs">

This will hook up the RESX files with ther CSharp code-behind files. That should take care of your problem with the country codes.

You are correct about there being no Role management in the standard interface. In fact, I was mostly talking about the actual process of registering a user. Yes, in fact, you must create all your own administrative pages for (after-the-fact) user and role management, unless you use the default ASP.NET configuration website, which again, is not branded. But, you should more or less copy the code from the default management website provided by ASP.NET on the back end to take care of handling all the business/security logic, unless your security/business requirements differ from the defaults provided by MS.

Anyway, I hope you are having success, otherwise. I ran into a bump in chapter 6 where the author checks for checkbox values in the ArticleController action EditArticle:


if (IsPostBack) {
  approved = approved ?? false;
  listed = listed ?? false;
  commentsEnabled = commentsEnabled ?? false;
  onlyForMembers = onlyForMembers ?? false;

That's a big no-no. First of all, controller's have no idea about whether a page is a postback, therefore, this property doesn't exist inside controllers. The author's explanation is that on POST, checkboxes that aren't checked do not provide any value. The workaround is to make changes to both the view and the controller.

In the CreateArticle.aspx view, change the following;


<h3>Acticle Options</h3>
<!-- Got rid of the P tag, invalid XHTML having a UL nested inside a P -->
<ul class="field input options">
  <!-- Note, in MVC 2.0, the tag <%: by default HTML encodes the output
      so you don't need to call Html.Encode yourself. You should always
      use this in place of <%=. Again, this is MVC 2.0 only!
    <%: Html.CheckBox("approved", new { value = "true" }) %>
    <label for="approved">Approved to be published</label>
  <!-- Notice, I used an overloaded Html.CheckBox constructor which takes
      an anonymous object representing HTML element attributes.
      This will make sure that check boxes return the value "true" when
      they are checked, otherwise, they will return nothing.

      Repeat this for the other 3 checkboxes.

Now, in your EditArticle action in your ArticleController, remove the "if" block so that it looks like the following:


approved = approved ?? false;
listed = listed ?? false;
commentsEnabled = commentsEnabled ?? false;
onlyForMembers = onlyForMembers ?? false;

This should work. You probably don't need to set the value attribute to "true" for the checkboxes in the view, but it should work either way.

nberardi May 30th, 2010 06:23 PM


Make sure you know what you are doing changing HttpPostOnly to HttpPost. They are not a shot by shot remake, and definitely have different functionality.

HTTP Post Only will only accept POST against the action. HTTP Post says that it prefers a POST when there is other actions with the same action name. But it will also accept GET and anything else.

Make sure you don't interpret, HttpPost as a security measure, because HttpPostOnly was a security measure against other types of requests. You will need to protect your action method in a different manor against GET and other requests that you don't want to allow.


fourpastmidnight May 30th, 2010 09:19 PM

MVC 2.0 Change Document says otherwise?

You know, I followed the discussion earlier in this post regarding this, and I agreed with you then. Now, I'm not really sure what the full story is.

Now, in version MVC 1.0, this "may" have been true (most certainly it was true for MVC 1.0 PR, which the book uses, I don't doubt that). However, in MVC 2.0, this is a new change. (maybe?) From what I've read, HttpPostAttribute only accepts HTTP POST verbs according to the changes FAQ on asp.net: http://www.asp.net/learn/whitepapers...t-mvc#_TOC3_12.

I'm open to further discussion on this, as it's important that if we're doing something for security, that it is in fact secure. There's nothing worse than a false sense of security.

Of course, I guess I should crack open .Net Reflector and look at the HttpPostAttribute and compare it to ManagedFusion.Web.Mvc.HttpPostOnlyAttribute.

fourpastmidnight May 30th, 2010 09:32 PM

Chapter 6: ViewArticle.aspx, Comment and foreach problem
I'd appreciate if anyone could help me out.

I'm at the part in the book where I'm creating the ViewArticle view (ViewArticle.aspx). I'm having a problem with the following code:


<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site/Site.Master" Inherits="System.Web.Mvc.ViewPage<TheBeerHouse.Models.Article>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <!-- There's some intermediate HTML/ASP.NET MVC nuggets here which are not pertinent. -->
    <div id="article-comments">
        <% foreach (TheBeerHouse.Models.Comment comment in ViewData.Model.Comments)
              Html.RenderPartial("CommentItem", comment);
    <!-- More stuff here. -->

The problem is with the foreach loop. First, the source code from codeplex doesn't even use the fully qualified name for Comment in the foreach loop, but if I don't use the fully qualified name, Comment is not recognized as a type. This occurs in several files in my project. I haven't had a problem with this until now. The foreach loop complains that it "cannot operate on variables of type System.Data.Linq.EntitySet<TheBeerHouse.Models.Com ment> because ....[it] does not contain a public definition for GetEnumerator". Um, first, I thought that what had to implement GetEnumerator was TheBeerHouse.Models.Article.Comments.... I'm confused.

All times are GMT -4. The time now is 10:22 PM.

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