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
  #1 (permalink)  
Old July 1st, 2011, 07:15 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 NerdDinner Search function (not AJAX pls)

I'm a classic ASP programmer (VB) and trying to learn ASP.NET MVC c# from this book and I'm really struggling...I know I am trying to run before walking but no one else could recommend a better book to me

In order for me to achieve my appraisal target I really must finish off this simple application before Sept. So any help is very much appreciated....

I'm building this simple gym attendance booking system based on the Nerd Dinner. I've pretty much got there except the Search function. I don't need the pretty AJAX function in this app. (like NerdDinner) but I need to be able to seach on 3 fields and return the records from one table -yes, just that simple.

However with Nerd Dinner, it uses AJAX and it's a lot more complicated than I need at this stage. Could anyone help me?

Problem:
I have created a Find.aspx form, very simplier to the Create.aspx but with a lot less fields.
In the Repository.cs, I have:
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 will add the other 2 seach fields once this is working)

In the Controller.cs, I have two actions:
public ActionResult Find()
{
Attendance attendance = new Attendance()
{
GA_Date = DateTime.Now.AddDays(0),
};
return View(new AttendanceFormViewModel(attendance));
}

//POST: /Attendance/Post
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Find(Attendance attendance)
{
var attn = attnRepository.FindAttendance(attendance.GA_Date);
if (attn == null)
return View("NotFound");
else
return View("Index");
}

The find page initially displays but when I click on seach, error page is displayed instead. I would really like to just use the index.aspx and display the searched records.

Surely this is a very simple function but I just cannot get it to work.

Please could someone help me?

Last edited by bh81; July 1st, 2011 at 07:18 AM.
Reply With Quote
  #2 (permalink)  
Old July 1st, 2011, 08:05 AM
Imar's Avatar
Wrox Author
Points: 66,632, Level: 100
Points: 66,632, Level: 100 Points: 66,632, Level: 100 Points: 66,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,124
Thanks: 65
Thanked 1,386 Times in 1,366 Posts
Default

Hi there,

What's the error you're getting?

Since your Get view uses a view model rather than an Attendance instance, shouldn't your POST Find method accept a view model as well?

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
  #3 (permalink)  
Old July 1st, 2011, 08:38 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

Thanks Imar for your prompt response.

It just goes straight to "sorry, an error occurred while processing your request." which is the standard error.aspx page in the Shared folder.

If I replace:

return View("Index");
with
return View("InvalidOwner");

to test if attn==null, it does call up the InvalidOwner.aspx regardless.

What do you mean by "shouldn't your POST Find method accept a view model as well?" Do you have any sample codes?

Please be gentle with me as I'm a classic ASP programmer and never exposed to ASP.NET or C# I'm learning this whole new thing in a hard way, but I'm good at looking at codes and tweaking them. (no one seems to be able to recommend a good book to fill in my knowledge gap so far:(). Thanks once again for your patience.
Reply With Quote
  #4 (permalink)  
Old July 1st, 2011, 10:36 AM
Imar's Avatar
Wrox Author
Points: 66,632, Level: 100
Points: 66,632, Level: 100 Points: 66,632, Level: 100 Points: 66,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,124
Thanks: 65
Thanked 1,386 Times in 1,366 Posts
Default

Maybe you can debug the code and see where the problem occurs? Just click on the first line you want to debug, then press F9 to set a break point and hit F5 to start debugging.

Your Find method returns a view which gets its data from AttendanceFormViewModel. I don't know what this method does but I assume it transforms your Attendance instance into a AttendanceFormViewModel. It would then make sense to have the post version of Find accept a similar view model E.g.:

public ActionResult Find(NameOfYourViewModel attendance)

where NameOfYOurViewModel should be replaced with the name of your view model - e.g. the type returned from the method you call in the regular Find method.
But it's only a guess. Knowing the real error would be very helpful.

Wrox also has a Beginning MVC book that targets MVC 1. Slightly outdated, but you'll learn many of the basic concepts for MVC. For a deeper dive, Steven Sanderson's MVC books are interesting too, although probably even more advanced than the one you're reading right now.

Cheers,

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
  #5 (permalink)  
Old July 4th, 2011, 07:51 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

Thansk Imar.

I have tried debugging but the application just wouldn't load. The debug (in VS2010) used to work but now when I do F5, the output of the mssage is:

The thread 'Agent: state execution thread for test 'Index' with id '06aaaacc-f6c6-445e-bd7c-43de04b1463d'' (0x104c) has exited with code 0 (0x0).
The thread 'Agent: adapter run thread for test 'About' with id 'ba1d7fe2-1469-41c7-8aed-b67e0714de54'' (0x1734) has exited with code 0 (0x0).
The thread 'Agent: state execution thread for test 'About' with id 'ba1d7fe2-1469-41c7-8aed-b67e0714de54'' (0x12c8) has exited with code 0 (0x0).
The thread 'Agent: test queue thread' (0x1024) has exited with code 0 (0x0).
The thread 'Agent: heartbeat thread' (0x1328) has exited with code 0 (0x0).
The program '[4104] QTAgent32.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

Not sure what they are. Hence it never gets to my breakpoint.
Reply With Quote
  #6 (permalink)  
Old July 4th, 2011, 10:09 AM
Imar's Avatar
Wrox Author
Points: 66,632, Level: 100
Points: 66,632, Level: 100 Points: 66,632, Level: 100 Points: 66,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,124
Thanks: 65
Thanked 1,386 Times in 1,366 Posts
Default

Not sure what's going on as I have never seen that specific error message before. Have you tried rebuilding the application? Could there be a compile error somewhere?

Also, the error message seems to suggest there's an issue with your unit tests. Could that be the problem?

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
  #7 (permalink)  
Old July 6th, 2011, 07:41 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

Thanks Imar for your comprehensive explanation. My debug is working now, not sure how it was fixed, my server admin put out some weekly updates and restarted the server and now we're back to where we were. Now to continue...

Perhaps I should just explain what I would like:
When a user selects "FIND", it should just displays a a blank screen with just 3 fields for him to fill in. Then he selects FIND and it just goes off and call up the Find method and returns the matched records.

I might have all the Find methods wrong so please feel free to scrap what I've done so far on the Find.

In my (initial) Find method which calls up my Find.aspx, the method is:
public ActionResult Find()
{
Attendance attendance = new Attendance()
{
GA_Date = DateTime.Now.AddDays(0),
};
return View(new AttendanceFormViewModel(attendance));
}
I was only copying the Create method similar to the NerdDinner. You could imagine that I almost literally rename "Dinner" to "Attendance". So whatever DinnerFormViewModel(dinner) does, it does very similar thing here.

Then I have the Post Find method trying to capture what criteria the user has entered and calls up the FindAttendance method:
//POST: /Attendance/Post
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Find(Attendance attendance)
{

var attn = attnRepository.FindAttendance(attendance.GA_Date);
if (attn == null)
return View("NotFound");
else
return View("CK");
}

The FindAttendance method in the AttnRepository.cs is below:
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;
}

When I debug this method, it appears that SelDate is being passed successfully.

I have temporarily created a "CK" page, similar to the index.aspx in NerdDinner except it has no paginated functions. Code in this page is:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<GymAttendance.He lpers.PaginatedList<GymAttendance.Models.Attendanc e>>" %>

<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.ToShortDateStrin g()) %>
<%=Html.Encode(attendance.GA_Time_From.ToString ()) %>
To
<%=Html.Encode(attendance.GA_Time_To.ToString ()) %>
</li>
<% } %>
</ul>


</asp:Content>

When I debug it sails through until it gets to CK.aspx at the "foreach (var attendance in Model) with error:

"Object reference not set to an instance of an object."

I think it's complaining about the "Model", like what you've suggested but I'm not sure how to rectify the problem.

Also thanks for your suggestion re. the book, I have already ordered it and I will read up on it once it arrives. But meanwhile please bear with me as I still hasn't grasped this whole Model thing.

Many thanks in advance Imar. You're very helpful.
Reply With Quote
  #8 (permalink)  
Old July 6th, 2011, 02:33 PM
Imar's Avatar
Wrox Author
Points: 66,632, Level: 100
Points: 66,632, Level: 100 Points: 66,632, Level: 100 Points: 66,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,124
Thanks: 65
Thanked 1,386 Times in 1,366 Posts
Default

If thats the case, wouldn't it be easier to have the first Find do nothing and let the second one (the Post) version accept some simple search model? E.g. (pseudo code)

Code:
 
public class FindViewModel
{
  public string LastName {get;set;}
  public string FirstName {get;set;}
}
 
 
.... Controller
public ActionResult Find()
{
  return View()
}
 
[HttpPost]
public ActionResult Results(FindViewModel find)
{
  var searchResults = search goed here
  return View(searchResults);
}
Then your Find View should have text fields that map to LastName and FirstName, and your Results view could accept some type of IEnumerable<whateverYouAreDisplaying)

Alternatively, you could combine them into a single view and model that has both search criteria and the results. E.g.:

Code:
 
public class FindViewModel
{
  public string LastName {get;set;}
  public string FirstName {get;set;}
  public IEnumerable<whateverYouAreDisplaying> {get; set;}
}
Hope this helps,

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
  #9 (permalink)  
Old July 7th, 2011, 05:43 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

Once again thank you for being so patient, Imar.

I have implemented what you have suggested (to my best knowledge):

In AttendanceController.cs, I have:
public class FindViewModel
{
public DateTime SelDate { get; set; }
public string SelTimeFrom { get; set; }
public string SelTimeTo { get; set; }
}

at the top, then further down in this:

public ActionResult Find()
{
return View();
}

It does calls the right page etc.

With the post method:
//POST: /Attendance/Post
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Find(FindViewModel find)
{

var attn = attnRepository.FindAttendance(find.SelDate);
if (attn == null)
return View("NotFound");
else
return View("CK");

NOTE: the CK view is only a temporary measure.

However it fails at var attn = attnRepository.FindAttendance(find.SelDate);
error message: "Object reference not set to an instance of an object."
it appears that "find.SelDate" is null.

I'm not sure how to set the public variable "public DateTime SelDate { get; set; }" to this search date on find.aspx

The current find.aspx is:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<GymAttendance.Co ntrollers.AttendanceFormViewModel>" %>

<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" %>
<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" />

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

<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>
<label for="GA_Date">Attendance Date:</label>
<%= Html.TextBox("GA_Date")%>
<%=Html.ValidationMessage("GA_Date", "*")%>
</p>
<p>
<label for="GA_Time_From">Time From:</label>
<%= Html.TextBox("GA_Time_From")%>
<%=Html.ValidationMessage("GA_Time_From", "*")%>
</p>
<p>
<label for="GA_Time_To">Time To:</label>
<%= Html.TextBox("GA_Time_To")%>
<%=Html.ValidationMessage("GA_Time_To", "*")%>
</p>

<p>

<input name="Find" type="submit" value="Find" />
</p>
</fieldset>

<% } %>

</asp:Content>

sorry if I sound daft but as I said earlier I still hasn't fully grasped this whole new "strategy". Thanks in advance once again.
Reply With Quote
  #10 (permalink)  
Old July 7th, 2011, 06:04 PM
Imar's Avatar
Wrox Author
Points: 66,632, Level: 100
Points: 66,632, Level: 100 Points: 66,632, Level: 100 Points: 66,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,124
Thanks: 65
Thanked 1,386 Times in 1,366 Posts
Default

Hi there,

Either I am not sure what you're trying to accomplish, ot you're mixing up the models. Here's what I have in mind:

1. There's a Get ActionMethod called Find. It renders a Find view that presents three controls mapped to the "search criteria".

2. There's a Post method (where you post to from the View in step 1) that accepts a prepulated copy of the FindViewModel

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

In order for this to work, you can use most of your code as is. However, you need to update the first Find view. Currently, it's tied to a AttendanceFormViewModel, but you want it to be of type FindViewModel if you want it to post back to the Post Find method. This worked for me:

Code:
 
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.FindViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  Find
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  <h2>Find</h2>
  <% using (Html.BeginForm())
     {%>
  <%: Html.ValidationSummary(true) %>
  <fieldset>
    <legend>Fields</legend>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.SelDate) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.SelDate) %>
      <%: Html.ValidationMessageFor(model => model.SelDate) %>
    </div>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.SelTimeFrom) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.SelTimeFrom) %>
      <%: Html.ValidationMessageFor(model => model.SelTimeFrom) %>
    </div>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.SelTimeTo) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.SelTimeTo) %>
      <%: Html.ValidationMessageFor(model => model.SelTimeTo) %>
    </div>
    <p>
      <input type="submit" value="Create" />
    </p>
  </fieldset>
  <% } %>
  <div>
    <%: Html.ActionLink("Back to List", "Index") %>
  </div>
</asp:Content>
This code posts back to the Post version of Find and the control values (such as SelTimeTo) are assigned to the FindViewModel instance.

Does that help?

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
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 07:30 AM.


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