locked
New property will not display RRS feed

  • Question

  • User-9156515 posted
    I am trying to display a new property from my model. Using the code listed here, the validation works great, but the Age will not show. Am I missing something here?
    1    public partial class Student
    2    {
    3        partial void OnDOBChanging(DateTime value)
    4        {
    5            if(value > DateTime.Now.AddYears(-18))
    6                throw new ArgumentException(string.Format("{0} must be at least 18 years old to participate", this.Name));
    7        }
    8    
    9        public int Age
    10       {
    11           get
    12           {
    13               TimeSpan t = DateTime.Today.Subtract(this.DOB);
    14   
    15               return Convert.ToInt32(Math.Floor(t.Days / 364.25));
    16           }
    17       }
    18   
    19    
    20               
    21   }
    22   
    
     
    Friday, April 25, 2008 8:43 AM

Answers

  • User-797310475 posted

    In that case, you need to provide a UIHintAttribute for that field. In this case, you can declare it on you custom property directly without havign to go through the metadata proxy class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 25, 2008 1:44 PM

All replies

  • User461194179 posted

    Hi,

    Where you are passing Age?

    There is no line of code which is storing Age.

    Friday, April 25, 2008 9:01 AM
  • User-9156515 posted

    Age is not stored. It is calculated from DOB, which comes from the Linq2sql model and and DateTime.Now.  


    Friday, April 25, 2008 9:24 AM
  • User461194179 posted

    Yeah, its calculating but you are not passing Age in your code then how it should display?

    You have written Property but not used it in your above code.

    Friday, April 25, 2008 9:46 AM
  • User-9156515 posted

    I guess that is my question then. When I use this class on a Gridview connected to a LinqToSqlDataSource, it works fine. 

    I thought Dynamic Data would interact with the model in the same fashion.

    How would you display a calculated field using Dynamic Data?
     

    Friday, April 25, 2008 9:58 AM
  • User461194179 posted

    Hi,

    Try Passing

    OnDOBChanging(Age);

    in your DOB_TextChanged Changed Event.

    Friday, April 25, 2008 10:01 AM
  • User-9156515 posted

    First, thank you for taking interest in my question. Your help is very much appreciated. I don't mean to sound disrespectful.

    I apologize for making this so confusing. Let me restate the question:

    The OnDBChanging method was there just to validate input. Lets take it out entirely.

    I have a "Student" table in my database that has a DOB (date of birth) date column.

    I have created a partial class called Student in my application that extends the Student class created by LinqToSql.

    I am adding a new property called Age to the partial class that provides the age (in years) of the student.

    Using the Dynamic Data Preview,  when I  navigate to /WebSite1/Students/Details.aspx?StudentId=1 I'd like to see a row displaying the value of the calculated Age.

    It does not show up.

    I think I may be missing some Attribute, but I can't find it in the documentation.

     

    Friday, April 25, 2008 10:15 AM
  • User-797310475 posted

    Hi ver_bal,

    you are correct and I apologize for any confusion or frustration. Your custom property should be automatically discovered by the Dynamic Data runtime and displayed as an additional column in the grid view. Are you using the default scaffolded pages or did you write any custom ones?

     

    And to Kumar,

    this question refers to a new feature called ASP.NET Dynamic Data and is quite valid in that context. You can learn more about it in this intro post by David and download the latest preview release at code gallery.
     

    Friday, April 25, 2008 11:55 AM
  • User-9156515 posted

     marcind,

     I'm using default scaffolded pages.
     

    Friday, April 25, 2008 1:16 PM
  • User-797310475 posted

    In that case, you need to provide a UIHintAttribute for that field. In this case, you can declare it on you custom property directly without havign to go through the metadata proxy class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 25, 2008 1:44 PM
  • User-9156515 posted

    Nailed it! [High Five]

    Friday, April 25, 2008 1:49 PM
  • User-797310475 posted

    Actually, using ScaffolAttribute(true) would be even better, as that way you can rely on the dynamic data runtime to pick the right field template for you without having to hardcode a reference to it via the uihint

    Friday, April 25, 2008 2:07 PM
  • User-9156515 posted

    Yes. Even better. 

    Friday, April 25, 2008 2:37 PM
  • User461194179 posted

     Hi marcind,

    Thanks for your information. 

    Monday, April 28, 2008 1:23 AM
  • User-803669149 posted

    Hi there

    I am having the same problem you described, but with VS2008 SP1 using an Entity Model and not a Linq to Sql model. I have tried just about everything to get the PageCount property to display in Dynamic Data. My code is as follows:

    1    [MetadataType(typeof(SiteMetadata))]
    2    [DisplayName("Site Settings")]
    3    public partial class Site
    4    {
    5    	public string PageCount
    6    	{
    7    		get
    8    		{
    9    			if (this.Pages == null)
    10   				return 0.ToString();
    11   			return this.Pages.Count.ToString();
    12   		}
    13   	}
    14   }
    15   
    16   public class SiteMetadata
    17   {
    18   	[ScaffoldColumn(false)]
    19   	public object SiteId { get; set; }
    20   
    21   	[DisplayName("Site Name")]
    22   	public object SiteName { get; set; }
    23   
    24   	[DataTypeAttribute(DataType.Text),
    25   	ScaffoldColumn(true),
    26   	UIHint("Text"),
    27   	DisplayName("Page Count")]
    28   	public object PageCount { get; set; }
    29   }
    
     
    Friday, August 22, 2008 9:37 AM
  • User-383448179 posted

    I am not able to repro your problem. I tried to use the custom column that you specified. What is the exact response that you are seeing?

    Make sure that you recompile you website after you make changes to your partial class.

    Friday, August 22, 2008 6:57 PM
  • User-803669149 posted

    Hi there

    Let me explain in detail what I have done on a brand new installation of Windows Vista with VS2008 and SQL 2005:

    1. I installed the VS2008 SP1 RTM and rebooted my machine as directed by the SP installation app.
    2. Fired up VS and created a Dynamic Data Entities Web Application using C#.
    3. Added an ADO.NET Entity Data Model for a database I have on my machine.
    4. Update the Global.asax file by changing the RegisterContext for the new Model and set the ScaffoldAllTables = true.
    5. Built and ran the application and all works great!

    So thus far there is no problem - let me repeat NO PROBLEM!

    I want to now modify my entity model by adding a calculated read only property which calculates the number of jobs to the customer entity.

    Here is where the problem starts:

    1. I add a new class file to the project. I make sure the namespace of the new file is the same as the entity model.
    2. The class file is made puplic partial and looks like this: 
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      
      namespace TestDD
      {
          [MetadataType(typeof(lstCustomerMetadata))]
          public partial class lstCustomer
          {
              public int JobsOutstanding 
              { 
                  get
                  {
                      //Do calculation on outstanding jobs
                      return 10;
                  }
              }
          }
      
          public class lstCustomerMetadata
          {
              [ScaffoldColumn(true)]
              [UIHint("Text")]
              public object JobsOutstanding { get; set; }
          }
      }
       
    3. I rebuild the project and run the Dynamic Data application.
    4. When I go to link for the Customer table (link, view, and edit), the column is not being displayed.

    When I write a Linq query, the column is in the query and all works fine. 

    Does anyone know if this is a bug with Dynamic Data or am I missing some additional information?

    Thanks

     

    Darrel

    Saturday, August 23, 2008 12:34 PM
  • User-330204900 posted

    Here is where the problem starts:

    1. I add a new class file to the project. I make sure the namespace of the new file is the same as the entity model.
    2. The class file is made puplic partial and looks like this: I rebuild the project and run the Dynamic Data application.
    3. When I go to link for the Customer table (link, view, and edit), the column is not being displayed.

    When I write a Linq query, the column is in the query and all works fine. 

    Does anyone know if this is a bug with Dynamic Data or am I missing some additional information?

    Hi Darrel, I've just tried again what you have done but with Linq to SQL and all seems well for me I will try it with EF tomorrow and let you know, but in the mean time try it with Linq to SQL and if the problem still persists it may be your machine [:(]

    But I will try it with a simple EF model tomorrow and get back to you [:D]

    Hope this helps [:D]

    Sunday, August 24, 2008 6:17 PM
  • User-330204900 posted

    I've just replicated the results with EF.

    So the issue appears to be with EF as scaffolded conputed columns do NOT appear to work with EF but do with Linq to SQL

    Hope this helps [:D]

    Sunday, August 24, 2008 6:38 PM
  • User-330204900 posted

    See this post by Marcin

    [:D]

    Monday, August 25, 2008 6:02 AM