 |
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 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
|
|
|
|
|

July 8th, 2011, 05:42 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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.
|
|

July 8th, 2011, 06:32 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

July 8th, 2011, 06:48 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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?
|
|

July 8th, 2011, 08:03 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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.
|
|

July 8th, 2011, 08:24 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

July 8th, 2011, 09:01 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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. 
|
|

July 8th, 2011, 09:37 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 11th, 2011, 04:16 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Imar
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 
|
|

July 11th, 2011, 04:23 AM
|
|
Authorized User
|
|
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Imar
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....
|
|
 |