Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > BOOK: Professional ASP.NET MVC 1.0 ISBN: 978-0-470-38461-9
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET MVC 1.0 ISBN: 978-0-470-38461-9
This is the forum to discuss the Wrox book Professional ASP.NET MVC 1.0 by Rob Conery, Scott Hanselman, Phil Haack, Scott Guthrie; ISBN: 978-0-470-38461-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 1.0 ISBN: 978-0-470-38461-9 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #11 (permalink)  
Old July 8th, 2011, 05:42 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thank you Imar again. I think I'm getting a bit closer to the solution but still not quite there yet.

Following through your 3 bullet points:
1. There's a Get ActionMethod called Find. It renders a Find view that presents three controls mapped to the "search criteria".

In my AttendanceController.cs, I have this method:
Code:
        public ActionResult Find()
        {
            return View();
        }
2. There's a Post method (where you post to from the View in step 1) that accepts a prepulated copy of the FindViewModel

In my AttendanceController.cs, I have this method:

Code:
  //POST: /Attendance/Post
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Find(FindViewModel find)
        {
            var attn = attnRepository.FindAttendance(find.GA_Date);
            if (attn == null)
                return View("NotFound");
            else
                return View("CK");
        }
3. This post method then searches the repository and renders the "CK" view (whatever this is, but I assume it can render one or more attendances).

The repository is in my AttnRepository.cs:
Code:
        public IQueryable<Attendance> FindAttendance(DateTime SelDate)
        {

            return from attendance in db.Attendances
                   where attendance.GA_Date >= SelDate
                   orderby attendance.GA_Date, attendance.GA_Time_From
                   select attendance;
        }
I have moved my FindViewModel into my Attendance.cs under the Models folder:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Web.Mvc;
using GymAttendance.Helpers;

namespace GymAttendance.Models
{
    public class FindViewModel
    {
        public DateTime GA_Date { get; set; }
        public string GA_Time_From { get; set; }
        public string GA_Time_To { get; set; }    }
}
Find.aspx:
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<GymAttendance.Models.FindViewModel>" %>

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
    Find Attendance
</asp:Content>

<asp:Content ID="Find" ContentPlaceHolderID="MainContent" runat="server">
    <link type="text/css" href="/Content/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
    <link type="text/css" href="/Content/timePicker.css" rel="stylesheet" />
    <h2>Find Attendance</h2>

<%=Html.ValidationSummary("Please correct the errors and try again") %>
<%@ Import Namespace="GymAttendance.Helpers" %>


<% using (Html.BeginForm()) { %>

<%: Html.ValidationSummary(true) %>

   <fieldset>
        <script type="text/javascript" src="/Scripts/jquery-1.5.1.min.js"></script>
        <script type="text/javascript" src="/Scripts/jquery-ui-1.8.11.custom.min.js"></script>
        <script type="text/javascript" src="/Scripts/jquery.timePicker.js"></script>

        <script language="javascript">
            $(document).ready(function () {
                $('#GA_Date').datepicker({ dateFormat: '<%= Html.ConvertDateFormat() %>' });
                $("#GA_Time_From").timePicker({
                    startTime: "07.30", // Using string. Can take string or Date object.
                    endTime: new Date(0, 0, 0, 20, 30, 0), // Using Date object here.
                    show24Hours: true,
                    separator: '.',
                    step: 15
                });
                $("#GA_Time_To").timePicker({
                    startTime: "07.30", // Using string. Can take string or Date object.
                    endTime: new Date(0, 0, 0, 20, 30, 0), // Using Date object here.
                    show24Hours: true,
                    separator: '.',
                    step: 15
                });
            }); 
        </script> 
       <p>
<% using (Html.BeginForm())
     {%>
  <%: Html.ValidationSummary(true) %>

    <legend>Fields</legend>
    <div class="editor-label">

      <%: Html.LabelFor(model => model.GA_Date)%>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.GA_Date)%>
      <%: Html.ValidationMessageFor(model => model.GA_Date)%>
    </div>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.GA_Time_From) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.GA_Time_From)%>
      <%: Html.ValidationMessageFor(model => model.GA_Time_From)%>
    </div>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.GA_Time_To) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.GA_Time_To)%>
      <%: Html.ValidationMessageFor(model => model.GA_Time_To)%>
    </div>
    <p>
      <input name="Find" type="submit" value="Find" />
    </p>
  </fieldset>
  <% } %>
  <div>
    <%: Html.ActionLink("Back to List", "Index") %>
  </div>
<% } %>

</asp:Content>
CK.aspx:
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<GymAttendance.Helpers.PaginatedList<GymAttendance.Models.Attendance>>" %>

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Advance Gym Attendance</h2>

    <ul>
        <%foreach (var attendance in Model)
          { %>
          <li>
            <%=Html.ActionLink(attendance.GA_Attendee,"Details",new {id=attendance.GA_ID}) %>
            attending on
            <%=Html.Encode(attendance.GA_Date.ToShortDateString()) %>
             <%=Html.Encode(attendance.GA_Time_From.ToString ()) %>
             To
             <%=Html.Encode(attendance.GA_Time_To.ToString ()) %>
          </li>
        <% } %>
    </ul>
</asp:Content>
I think I have pasted all the related codes??

When I debug it, it still fails at the same point (the post method) - in AttendanceController.cs, [AcceptVerbs(HttpVerbs.Post)]
public ActionResult Find(FindViewModel find)
at line: var attn = attnRepository.FindAttendance(find.GA_Date);
error still says:
Quote:
Object reference not set to an instance of an object.
Why is find.GA_Date still not "bound"?

Thanks again in advance.
Reply With Quote
  #12 (permalink)  
Old July 8th, 2011, 06:32 AM
Imar's Avatar
Wrox Author
Points: 66,779, Level: 100
Points: 66,779, Level: 100 Points: 66,779, Level: 100 Points: 66,779, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,153
Thanks: 65
Thanked 1,391 Times in 1,371 Posts
Default

What happens if (for now) you simplify the Find view by removing the custom jQuery stuff and test out the String properties only? When you set a breakpoint in the Find method, is the FindViewModel (find) null, or are its properties null?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #13 (permalink)  
Old July 8th, 2011, 06:48 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Default

OK, I have removed all my jquery stuff, and my find.aspx now looks like this:

Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<GymAttendance.Models.FindViewModel>" %>

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
    Find Attendance
</asp:Content>

<asp:Content ID="Find" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Find Attendance</h2>

<%=Html.ValidationSummary("Please correct the errors and try again") %>

<% using (Html.BeginForm()) { %>

<%: Html.ValidationSummary(true) %>

   <fieldset>
       <p>
<% using (Html.BeginForm())
     {%>
  <%: Html.ValidationSummary(true) %>

    <legend>Fields</legend>
    <div class="editor-label">

      <%: Html.LabelFor(model => model.GA_Date)%>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.GA_Date)%>
      <%: Html.ValidationMessageFor(model => model.GA_Date)%>
    </div>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.GA_Time_From) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.GA_Time_From)%>
      <%: Html.ValidationMessageFor(model => model.GA_Time_From)%>
    </div>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.GA_Time_To) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.GA_Time_To)%>
      <%: Html.ValidationMessageFor(model => model.GA_Time_To)%>
    </div>
    <p>
      <input name="Find" type="submit" value="Find" />
    </p>
  </fieldset>
  <% } %>
  <div>
    <%: Html.ActionLink("Back to List", "Index") %>
  </div>
 
<% } %>

</asp:Content>
When I debug
public ActionResult Find(FindViewModel find)
find has null value.
hence when I F5 again to
var attn = attnRepository.FindAttendance(find.GA_Date);
it throws out "Object reference not set to an instance of an object."
hence it appears FindViewModel find is not set.

What am I doing wrong?
Reply With Quote
  #14 (permalink)  
Old July 8th, 2011, 08:03 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi Imar, some good news. I was x-ref with the NerdDinner Changepassword module and I think I'm missing:
Quote:
using System.ComponentModel;
So now, I'm moving onto the CK.aspx:
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<GymAttendance.Helpers.PaginatedList<GymAttendance.Models.Attendance>>" %>

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Advance Gym Attendance</h2>

    <ul>
        <%foreach (var attendance in Model)
          { %>
          <li>
            <%=Html.ActionLink(attendance.GA_Attendee,"Details",new {id=attendance.GA_ID}) %>
            attending on
            <%=Html.Encode(attendance.GA_Date.ToShortDateString()) %>
             <%=Html.Encode(attendance.GA_Time_From.ToString ()) %>
             To
             <%=Html.Encode(attendance.GA_Time_To.ToString ()) %>
          </li>
        <% } %>
    </ul>
</asp:Content>
It fails on:
<%foreach (var attendance in Model)
with error: "Object reference not set to an instance of an object."
Model is null in this instance.

Have I inherit the right model? Not sure how to tackle this.

Thanks in advance again.
Reply With Quote
  #15 (permalink)  
Old July 8th, 2011, 08:24 AM
Imar's Avatar
Wrox Author
Points: 66,779, Level: 100
Points: 66,779, Level: 100 Points: 66,779, Level: 100 Points: 66,779, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,153
Thanks: 65
Thanked 1,391 Times in 1,371 Posts
Default

Quote:
and I think I'm missing:

using System.ComponentModel;
Then the code shouldn't even have compiled I think. Did you check the error list before you tried to run it?

As to your new problem, take a look at this:

Code:
var attn = attnRepository.FindAttendance(find.GA_Date);
            if (attn == null)
                return View("NotFound");
            else
                return View("CK");
You're only getting the attendances, but never pass them to the View method....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #16 (permalink)  
Old July 8th, 2011, 09:01 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Yes I did check the error list and it says 0Errors. I'm not sure why but it seems that after I have included:

using System.ComponentModel;

my "find" is not null anymore?!!!

I've updated my AttendanceController.cs to:
Code:
        //POST: /Attendance/Post
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Find(FindViewModel find, int? page)
        {
            const int pageSize = 10;
            var attn = attnRepository.FindAttendance(find.GA_Date);
            var paginatedAttn = new PaginatedList<Attendance>(attn, page ?? 0, pageSize);
            if (paginatedAttn.Count == 0)
            {
                return View("NotFound");
            }
            else
            {
                return View(paginatedAttn);
            }
Again I just copy the codes from the NerdDinner, and the PaginatedList here is exactly the same as the one in NerdDinner.

But when I debug to
"return View(paginatedAttn);"
it throws this error:

Quote:
The model item passed into the dictionary is of type 'GymAttendance.Helpers.PaginatedList`1[GymAttendance.Models.Attendance]', but this dictionary requires a model item of type 'GymAttendance.Models.FindViewModel'.
I was trying to reuse my index.aspx page:
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<GymAttendance.Helpers.PaginatedList<GymAttendance.Models.Attendance>>" %>

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Advance Gym Attendance</h2>

    <ul>
        <%foreach (var attendance in Model)
          { %>
          <li>
            <%=Html.ActionLink(attendance.GA_Attendee,"Details",new {id=attendance.GA_ID}) %>
            attending on
            <%=Html.Encode(attendance.GA_Date.ToShortDateString()) %>
             <%=Html.Encode(attendance.GA_Time_From.ToString ()) %>
             To
             <%=Html.Encode(attendance.GA_Time_To.ToString ()) %>
          </li>
        <% } %>
    </ul>

       <% if (Model.HasPreviousPage) {%>
            <%=Html.RouteLink("<<<", 
                            "AdvanceAttendance", 
                            new { page=(Model.PageIndex-1) }) %>
        <% } %>

        <% if (Model.HasNextPage) {%>
            <%=Html.RouteLink(">>>", "AdvanceAttendance", new { page = (Model.PageIndex + 1) })%>
        <% } %>

</asp:Content>
Is it possible? Or do I need to create another page. If latter, is the "Inherits" the only thing I need to change?

Thank you again for being extremely patience and helpful to me.
Reply With Quote
  #17 (permalink)  
Old July 8th, 2011, 09:37 AM
Imar's Avatar
Wrox Author
Points: 66,779, Level: 100
Points: 66,779, Level: 100 Points: 66,779, Level: 100 Points: 66,779, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,153
Thanks: 65
Thanked 1,391 Times in 1,371 Posts
Default

By calling this:

Code:
return View(paginatedAttn);
you execute the Find view which indeed expects a differnet model. Earlier you used the CK view which you should pass in here as well:

Code:
return View("CK", paginatedAttn);
This way, the paginatedAttn is rendered using the CK view....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
bh81 (July 11th, 2011)
  #18 (permalink)  
Old July 11th, 2011, 04:16 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
Then the code shouldn't even have compiled I think. Did you check the error list before you tried to run it?

As to your new problem, take a look at this:

Code:
var attn = attnRepository.FindAttendance(find.GA_Date);
            if (attn == null)
                return View("NotFound");
            else
                return View("CK");
You're only getting the attendances, but never pass them to the View method....

Imar
I have had new findings to this, I actually changed two "variables" for this experiment, I can conclude that it's the name = "Find" in
Quote:
<input name="Find" type="submit" value="Find" />
that caused the prolem not not the reference.

Sorry for the confusion and thanks for pointing this out
Reply With Quote
  #19 (permalink)  
Old July 11th, 2011, 04:23 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Thumbs up

Quote:
Originally Posted by Imar View Post
By calling this:

Code:
return View(paginatedAttn);
you execute the Find view which indeed expects a differnet model. Earlier you used the CK view which you should pass in here as well:

Code:
return View("CK", paginatedAttn);
This way, the paginatedAttn is rendered using the CK view....

Imar
Excellent Imar. Thanks a trillion. It's all working now. You're a saviour! Now I need to try two more search criteria....
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
XML XSLT AJAX and Database Advice Pls newbieboobers XSLT 2 March 20th, 2008 08:53 AM
sql count function - urgent and pls help miki SQL Language 1 June 14th, 2006 09:47 AM
New Ajax article: Creating an Ajax Search W jminatel BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 May 11th, 2006 03:45 PM
new Ajax article: Creating an Ajax Search Widget jminatel Ajax 0 May 11th, 2006 02:50 PM
sql count function - pls help and urgent miki MySQL 1 May 12th, 2004 05:55 AM



All times are GMT -4. The time now is 11:19 PM.


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