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
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 .
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; } }
}
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.
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?
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.
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'.