Wrox Programmer Forums
|
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
 
Old June 12th, 2009, 12:38 PM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to type a ViewPage as IList

On page 286 they list the contents of the partial but do not show how to type it as IList. If someone could provide a full listing of the page that would be great.

Thanks
Danny
 
Old June 12th, 2009, 01:00 PM
Authorized User
 
Join Date: Jun 2009
Posts: 20
Thanks: 0
Thanked 1 Time in 1 Post
Default Strongly Typing Views

Hi dannygr,
When you go to Add -> View from Solution Explorer, check the box that says 'Create strongly-typed view' and type in IList<[Namespace of the Product class].Product>.

...or if you open the partial page and look into the first line, it says something like Inherits='.....'. You could change it to Inherits="System.Web.Mvc.ViewUserControl<IList<[Namespace of the Product class].Product>>"

I hope this helps.

Regards,
S.

Last edited by sakshamgautam; June 12th, 2009 at 01:03 PM..
 
Old June 12th, 2009, 01:16 PM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error

I am getting an NullReferenceExeption on ViewData.Model:
<% foreach (MVCAjax.Models.Product p in ViewData.Model)

Have you a working sample a can have a look at?

Thanks
Danny
 
Old June 12th, 2009, 01:23 PM
Authorized User
 
Join Date: Jun 2009
Posts: 20
Thanks: 0
Thanked 1 Time in 1 Post
Default Sample

Are you sure that in your controller you are initializing an instance of products [page 285, ProductSearch(string query)] when returning ViewResult?

I'm currently reading the same chapter. Will post my example here when I get to that example.
 
Old June 12th, 2009, 01:36 PM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That would be great.

Thanks
Danny
 
Old June 12th, 2009, 05:06 PM
Authorized User
 
Join Date: Jun 2009
Posts: 20
Thanks: 0
Thanked 1 Time in 1 Post
Default Missing ViewData.Model Check

Hi Danny,
I see that the authors only checked for the number of elements in the ViewData.Model. However, it seems that they forgot to consider the case when the user requests the search page for the first time (ViewData.Model will be null in that case). Hence, if you replace
Code:
<%if(ViewData.Model.Count > 0) {
with
Code:
<%if(ViewData.Model != null && ViewData.Model.Count > 0) {
, it should work.

Here's the complete listing of ProductSearchResult.aspx partial view:

Code:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<MyMvc.Models.Product>>" %>

<% if (ViewData.Model != null && ViewData.Model.Count > 0)
   {%>
   <table cellpadding="5">
   <tr>
    <td><b>Product</b></td>
    <td><b>Price</b></td>
   </tr>
   
   <%foreach (MyMvc.Models.Product p in ViewData.Model)
     { %>
        <tr>
            <td><%= Html.Encode(p.ProductName) %></td>
            <td><%= p.UnitPrice %></td>
        </tr>
   <%} %>
   </table>
<% } %>
The View which invokes the controller and displays the result looks like this:
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Ajax Search
</asp:Content>

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

    <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

    <h2>Search AJAX</h2>
    <%using (Ajax.BeginForm("ProductSearch",
        new AjaxOptions { UpdateTargetId = "results" }))
      { %>
      
      <%= Html.TextBox("query", null, new {size=40}) %>
      <input type="submit" />
      
    <%} %>
    
    <div id="results">
        <%
            Html.RenderPartial("ProductSearchResults", ViewData.Model); 
        %>
    </div>
</asp:Content>
Note that my Product class is defined as MyMvc.Models.Product


Hope this helps!

Let me know if this solves your problem.

S.
 
Old June 13th, 2009, 09:24 AM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The checking for null sorted it, thanks. I had to use ViewPage in the partial though, if I used ViewUserControl, it results in a HttpCompileExeception : "no suitable method found to override" at Html.RenderPartial

Cheers
Danny
 
Old June 13th, 2009, 11:48 AM
Authorized User
 
Join Date: Jun 2009
Posts: 20
Thanks: 0
Thanked 1 Time in 1 Post
Default Partial View

...sorry I meant ProductSearchResult.ascx not ProductSearchResult.aspx because the ProductSearchResult should be a partial view as mentioned in the book.

Regards,
S.
 
Old June 13th, 2009, 12:06 PM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes thats sorted now

Cheers
Danny





Similar Threads
Thread Thread Starter Forum Replies Last Post
Guidelines Item 3: Replace System.Type with Type Ixtlia BOOK: Professional .NET 2.0 Generics 0 August 19th, 2007 04:09 AM
Conversion from type 'DBNull' to type 'Boolean' is steve35719 VB Databases Basics 4 June 29th, 2006 06:13 PM
Convert "String" type to "Control" type ? kishore_peddi C# 4 January 11th, 2006 01:21 PM





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