Я потерял больше своих волос, пытаясь решить эту проблему, поэтому я действительно надеялся, что вы поможете?
У меня есть сетка Telerik MVC:
И у меня есть собственный EditorTemplate для Address (Address.ascx)

Моя модель ImporterDetails:
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(),
};
}
}
И 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();
}
}
Вы заметите, что я исключил Штат и Страну, потому что, если я этого не сделаю, когда я вызываю TryUpdateModel (импортер) - я получаю страшное исключение конструктора без параметров. Мой вопрос:
Как мне получить правильный идентификатор штата и страны (или вообще любого раскрывающегося списка) в моих действиях таким образом?
Для полноты картины:
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>
Страна:
<%@ 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")%>
И государство идентично Стране, кроме очевидного.
Любая помощь?