locked
NerdDinner turorial: The name ' X ' does not exist in the current context RRS feed

  • Question

  • User-429092344 posted

    I am following step by step the ASP.NET NerdDinner tutorial and I get to this step http://www.asp.net/mvc/overview/older-versions-1/nerddinner/build-a-model-with-business-rule-validations.
    I did everything exactly as on tutorial (excepting the project name) and I'm getting the following errors:

        The name 'Title' does not exist in the current context
        The name 'Description' does not exist in the current context
        The name 'OrganisedBy' does not exist in the current context
        The name 'Address' does not exist in the current context
        The name 'Country' does not exist in the current context
        The name 'ContactPhone' does not exist in the current context    
        The name 'ContactPhone' does not exist in the current context

    Any ideas how to solve this?

    Trip.cs:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Text.RegularExpressions;
        using System.Data.Linq;
        
        namespace AdventureTrip.Models
        {
            public partial class Trip
            {
                public bool IsValid
                {
                    get { return (GetRuleViolations().Count() == 0); }
                }
        
                public IEnumerable<RuleViolation> GetRuleViolations()
                {
                    yield break;
                }
        
                partial void OnValidate(ChangeAction action)
                {
                    if (!IsValid)
                        throw new ApplicationException("Rule violation prevent saving");
                }
            }
        
            public class RuleViolation
            {
                public string ErrorMessage { get; private set; }
                public string PropertyName { get; private set; }
        
                public RuleViolation(string errorMessage)
                {
                    ErrorMessage = errorMessage;
                }
        
                public RuleViolation(string errorMessage, string propertyName)
                {
                    ErrorMessage = errorMessage;
                    PropertyName = propertyName;
                }
        
                public IEnumerable<RuleViolation> GetRuleViolations()
                {
                    if (String.IsNullOrEmpty(Title))
                        yield return new RuleViolation("Title required", "Title");
        
                    if (String.IsNullOrEmpty(Description))
                        yield return new RuleViolation("Description required", "Description");
        
                    if (String.IsNullOrEmpty(OrganisedBy))
                        yield return new RuleViolation("HostedBy required", "HostedBy");
        
                    if (String.IsNullOrEmpty(Address))
                        yield return new RuleViolation("Address required", "Address");
        
                    if (String.IsNullOrEmpty(Country))
                        yield return new RuleViolation("Country required", "Country");
        
                    if (String.IsNullOrEmpty(ContactPhone))
                        yield return new RuleViolation("Phone# required", "ContactPhone");
        
                    if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
                        yield return new RuleViolation("Phone# does not match country", "ContactPhone");
                    yield break;
                }
        
                public class PhoneValidator
                {
                    static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>()
                    {
                        { "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
                        { "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},
                        { "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},
                    };
                    public static bool IsValidNumber(string phoneNumber, string country)
                    {
                        if (country != null && countryRegex.ContainsKey(country))
                            return countryRegex[country].IsMatch(phoneNumber);
                        else
                            return false;
                    }
                    public static IEnumerable<string> Countries
                    {
                        get { return countryRegex.Keys; }
                    }
                }
            }
        }
        
     

    Wednesday, September 2, 2015 9:54 AM

Answers

  • User-429092344 posted

    Found the solution...move 'public IEnumerable<RuleViolation> GetRuleViolations()' and 'public class PhoneValidator' inside of class Trip

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Text.RegularExpressions;
        using System.Data.Linq;
        
        namespace AdventureTrip.Models
        {
            public partial class Trip
            {
                public bool IsValid
                {
                    get { return (GetRuleViolations().Count() == 0); }
                }
        
                public IEnumerable<RuleViolation> GetRuleViolations()
                {
                    if (String.IsNullOrEmpty(Title))
                        yield return new RuleViolation("Title required", "Title");
        
                    if (String.IsNullOrEmpty(Description))
                        yield return new RuleViolation("Description required", "Description");
        
                    if (String.IsNullOrEmpty(OrganisedBy))
                        yield return new RuleViolation("HostedBy required", "HostedBy");
        
                    if (String.IsNullOrEmpty(Address))
                        yield return new RuleViolation("Address required", "Address");
        
                    if (String.IsNullOrEmpty(Country))
                        yield return new RuleViolation("Country required", "Country");
        
                    if (String.IsNullOrEmpty(ContactPhone))
                        yield return new RuleViolation("Phone# required", "ContactPhone");
        
                    if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
                        yield return new RuleViolation("Phone# does not match country", "ContactPhone");
                    yield break;
                }
        

                public class PhoneValidator
                {
                    static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>()
                    {
                        { "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
                        { "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},
                        { "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},
                    };
                    public static bool IsValidNumber(string phoneNumber, string country)
                    {
                        if (country != null && countryRegex.ContainsKey(country))
                            return countryRegex[country].IsMatch(phoneNumber);
                        else
                            return false;
                    }
                    public static IEnumerable<string> Countries
                    {
                        get { return countryRegex.Keys; }
                    }
                }


                partial void OnValidate(ChangeAction action)
                {
                    if (!IsValid)
                        throw new ApplicationException("Rule violation prevent saving");
                }
            }
        
            public class RuleViolation
            {
                public string ErrorMessage { get; private set; }
                public string PropertyName { get; private set; }
        
                public RuleViolation(string errorMessage)
                {
                    ErrorMessage = errorMessage;
                }
        
                public RuleViolation(string errorMessage, string propertyName)
                {
                    ErrorMessage = errorMessage;
                    PropertyName = propertyName;
                }
            }
        }
       

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 3, 2015 7:53 AM

All replies

  • User2024324573 posted

    I did everything exactly as on tutorial (excepting the project name) and I'm getting the following errors:

    As you have changed the project name, please check the namespace of the model class and others - are you using them correctly or not? 

    It seems to be missing the definition of the class.

    Hope this will help.

    Wednesday, September 2, 2015 5:15 PM
  • User-429092344 posted

    I checked and the functionality is the same as in tutorial

    Thursday, September 3, 2015 3:40 AM
  • User-429092344 posted

    Found the solution...move 'public IEnumerable<RuleViolation> GetRuleViolations()' and 'public class PhoneValidator' inside of class Trip

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Text.RegularExpressions;
        using System.Data.Linq;
        
        namespace AdventureTrip.Models
        {
            public partial class Trip
            {
                public bool IsValid
                {
                    get { return (GetRuleViolations().Count() == 0); }
                }
        
                public IEnumerable<RuleViolation> GetRuleViolations()
                {
                    if (String.IsNullOrEmpty(Title))
                        yield return new RuleViolation("Title required", "Title");
        
                    if (String.IsNullOrEmpty(Description))
                        yield return new RuleViolation("Description required", "Description");
        
                    if (String.IsNullOrEmpty(OrganisedBy))
                        yield return new RuleViolation("HostedBy required", "HostedBy");
        
                    if (String.IsNullOrEmpty(Address))
                        yield return new RuleViolation("Address required", "Address");
        
                    if (String.IsNullOrEmpty(Country))
                        yield return new RuleViolation("Country required", "Country");
        
                    if (String.IsNullOrEmpty(ContactPhone))
                        yield return new RuleViolation("Phone# required", "ContactPhone");
        
                    if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
                        yield return new RuleViolation("Phone# does not match country", "ContactPhone");
                    yield break;
                }
        

                public class PhoneValidator
                {
                    static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>()
                    {
                        { "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
                        { "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},
                        { "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},
                    };
                    public static bool IsValidNumber(string phoneNumber, string country)
                    {
                        if (country != null && countryRegex.ContainsKey(country))
                            return countryRegex[country].IsMatch(phoneNumber);
                        else
                            return false;
                    }
                    public static IEnumerable<string> Countries
                    {
                        get { return countryRegex.Keys; }
                    }
                }


                partial void OnValidate(ChangeAction action)
                {
                    if (!IsValid)
                        throw new ApplicationException("Rule violation prevent saving");
                }
            }
        
            public class RuleViolation
            {
                public string ErrorMessage { get; private set; }
                public string PropertyName { get; private set; }
        
                public RuleViolation(string errorMessage)
                {
                    ErrorMessage = errorMessage;
                }
        
                public RuleViolation(string errorMessage, string propertyName)
                {
                    ErrorMessage = errorMessage;
                    PropertyName = propertyName;
                }
            }
        }
       

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 3, 2015 7:53 AM