Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET MVC 2
This is the forum to discuss the Wrox book Professional ASP.NET MVC 2 by Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery; ISBN: Professional ASP.NET MVC 2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 2 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
  #1 (permalink)  
Old April 4th, 2011, 09:10 PM
Registered User
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with Country DropDownList

The Edit form does not show the current country. It always shows the first. "USA"

I made the DinnerFormVieModel I went to Edit.aspx form and I made the following changes:



Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Model s.DinnerFormViewModel>" %>
. . .
. . .

<div class="editor-field">
<%: Html.DropDownListFor(m => m.Dinner.Country, Model.Countries) %>
<%: Html.ValidationMessageFor(m => m.Dinner.Country, "*") %>
</div>



Somebody know where is the error?
  #2 (permalink)  
Old April 5th, 2011, 10:51 AM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

My code looks like this:
Code:
<%: Html.LabelFor(m => m.Country) %>
<%: Html.DropDownListFor(m => m.Country, Model.Countries) %>                   
<%: Html.ValidationMessageFor(m => m.Country, "*") %>
  #3 (permalink)  
Old April 5th, 2011, 02:38 PM
Registered User
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by flyinhawaiian View Post
My code looks like this:
Code:
<%: Html.LabelFor(m => m.Country) %>
<%: Html.DropDownListFor(m => m.Country, Model.Countries) %>                   
<%: Html.ValidationMessageFor(m => m.Country, "*") %>
Thank you flyinhawaiian, but when I change the code the aplication is not able to perform any update (Edit.aspx) it always shows the following message:
Please correct the erros and try again.

The "DropDown" is in red color. Si I went to Dinner partial class and commented the following line:
Code:
namespace NerdDinner.Models
{
    [MetadataType(typeof(Dinner_Validation))]
    public partial class Dinner
    {
        //...
    }
    public class Dinner_Validation
    {
        [Required(ErrorMessage = "Title is required")]
        [StringLength(50, ErrorMessage = "Title may not be longer than 50 characters")]
        public string Title { get; set; }

        [Required(ErrorMessage = "Description is required")]
        [StringLength(265, ErrorMessage =
          "Description must be 256 characters or less")]
        public string Description { get; set; }

        [Required(ErrorMessage = "Address is required")]
        public string Address { get; set; }

        //[Required(ErrorMessage = "Country is required")]
        public string Country { get; set; }

        [Required(ErrorMessage = "Phone# is required")]
        public string ContactPhone { get; set; }
    }


}
When I run the application it shows the same error.
  #4 (permalink)  
Old April 5th, 2011, 02:58 PM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

Perhaps there is something wrong with DinnerFormViewModel.cs. Here's what mine looks like:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NerdDinner.Models
{
	public class DinnerFormViewModel
	{
		private static string[] _countries = new[] {
				"USA",
				"Afghanistan",
				"Akrotiri",
				"Albania",
				//... omitted for brevity
				"Zimbabwe"
			};


		// Properties
		public Dinner Dinner { get; set; }

		// Custom shape Dinner makes view a lot cleaner because so we can use m.Title instead of m.Dinner.Title
		public string Title
		{
			get	{ return Dinner.Title; }
			set { Dinner.Title = value; }
		}

		public DateTime EventDate
		{
			get { return Dinner.EventDate; }
			set { Dinner.EventDate = value; }
		}

		public string Description
		{
			get { return Dinner.Description; }
			set { Dinner.Description = value; }
		}

		public string Address
		{
			get { return Dinner.Address; }
			set { Dinner.Address = value; }
		}

		public string Country
		{
			get { return Dinner.Country; }
			set { Dinner.Country = value; }
		}


		public string ContactPhone
		{
			get { return Dinner.ContactPhone; }
			set { Dinner.ContactPhone = value; }
		}

		public double Latitude
		{
			get { return Dinner.Latitude; }
			set { Dinner.Latitude = value; }
		}

		public double Longitude
		{
			get { return Dinner.Longitude; }
			set { Dinner.Longitude = value; }
		}
		// end custom shape




		public SelectList Countries { get; private set; }

		// Constructor
		public DinnerFormViewModel(Dinner dinner)
		{
			Dinner = dinner;

			Countries = new SelectList(_countries, dinner.Country);
		}

	}
}


Similar Threads
Thread Thread Starter Forum Replies Last Post
dropdownlist for country,state and city in javascript simons24 Javascript 1 May 11th, 2009 03:01 PM
Detect country using php anshul PHP How-To 5 June 11th, 2008 03:46 PM
select country in drop down and state accordingly vinodonline2000 ASP.NET 2.0 Basics 4 September 11th, 2007 09:42 AM
Find Country from IP? ccc_storage Classic ASP Professional 3 June 5th, 2005 10:49 AM
How does a foreigner in his country buy the books? everming BOOK: Beginning Visual C++ 6 0 May 6th, 2005 01:14 AM





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