I’ve lost more of my hair trying to solve this problem, so I really was hoping you could help?
I have a Telerik MVC grid:
And I have a custom EditorTemplate for Address (Address.ascx)

My ImporterDetails model:
public class ImporterViewModel : IEnumerableViewModel
{
public int ImporterId { get; set; }
[Required]
[DisplayName("Importer Name *")]
public string ImporterName { get; set; }
[Required]
[DisplayName("Importer Address *")]
public Address ImporterAddress { get; set; }
public static ImporterViewModel CreateImporter()
{
return new ImporterViewModel
{
ImporterName = Guid.NewGuid().ToString().Substring(0, 5),
ImporterAddress = Address.CreateDummyAddress(),
};
}
}
And the AddressViewModel:
[Bind(Exclude = "State, Country")]
public class Address
{
public int AddressId { get; set; }
[DisplayName("Address Line 1")]
[Required]
public string Line1 { get; set; }
[DisplayName("Address Line 2")]
public string Line2 { get; set; }
[DisplayName("Postcode")]
[Required]
[RegularExpression(RegexConstants.AUSTRALIAN_POSTCODE_PATTERN, ErrorMessage = "Invalid post code")]
public string Postcode { get; set; }
[DisplayName("State")]
public State State { get; set; }
[DisplayName("Suburb")]
public string Suburb { get; set; }
[DisplayName("Country")]
public Country Country { get; set; }
[Required]
public int CountryId { get; set; }
[Required]
public int StateId { get; set; }
/// <summary>
/// Creates a new dummy instance of Address
/// </summary>
/// <returns></returns>
public static Address CreateDummyAddress()
{
return new Address
{
Country = ServiceLocatorFactory.GetCodeServiceLocator<Country>().Get(x => x.CodeValue.ToLower() == "canada"),
State = ServiceLocatorFactory.GetCodeServiceLocator<State>().Get(x => x.CodeValue.ToLower() == "nsw"),
Line1 = Guid.NewGuid().ToString().Substring(0, 15),
Line2 = Guid.NewGuid().ToString().Substring(0, 15),
Suburb = "Dandenong",
Postcode = "2606",
};
}
public string AddressStrings
{
get
{
return ToString();
}
}
public override string ToString()
{
// create a blank StringBuilder
var sb = new StringBuilder();
// add the first address line
sb.Append(string.Format("{0}, ", Line1));
// add the second address line
sb.Append(string.Format("{0}, ", Line2));
sb.Append(string.Format("{0}, ", Suburb));
sb.Append(string.Format("{0} {1}, ", State == null ? string.Empty : State.Description, Postcode));
sb.Append(string.Format("{0}", Country == null ? string.Empty : Country.Description));
// and then return it as a single (formatted) string
return sb.ToString();
}
}
You’ll notice I’ve excluded State and Country because if I don’t, when I call a TryUpdateModel(importer) – I get the dreaded parameterless constructor exception. My question is:
How do I go about getting the right id of the State and Country (or in general, any dropdown) in my action this way?
For completeness’ sake:
Address.ascx
<div class="formElementGroupVertical">
<%: Html.LabelFor(m => m.Line1) %>
<%: Html.EditorFor(m => m.Line1) %>
<%: Html.ValidationMessageFor(m => m.Line1) %>
</div>
<div class="formElementGroupVertical">
<%: Html.LabelFor(m => m.Line2) %>
<%: Html.EditorFor(m => m.Line2) %>
<%: Html.ValidationMessageFor(m => m.Line2) %>
</div>
<div class="formElementGroupVertical">
<%: Html.LabelFor(m => m.Suburb) %>
<%: Html.EditorFor(m => m.Suburb)%>
<%: Html.ValidationMessageFor(m => m.Suburb)%>
</div>
<div class="formElementGroupVertical">
<%: Html.LabelFor(m => m.State) %>
<%: Html.EditorFor(m => m.State) %>
</div>
<div class="formElementGroupVertical">
<%: Html.LabelFor(m => m.Postcode) %>
<%: Html.EditorFor(m => m.Postcode)%>
<%: Html.ValidationMessageFor(m => m.Postcode)%>
</div>
<div class="formElementGroupVertical">
<%: Html.LabelFor(m => m.Country) %>
<%: Html.EditorFor(m => m.Country) %>
</div>
Country:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Web.Common.Models.Country>" %>
<%@ Import Namespace="Web.Common.Models" %>
<%@ Import Namespace="Web.Common.Service" %>
<%: Html.DropDownListFor(m => m.CodeId, new SelectList(ServiceLocatorFactory.GetCodeServiceLocator<Country>().GetAll(), "CodeId", "Description"), "Please Select")%>
And state is Identical to Country except the obvious.
Any help?