Doug trả lời câu hỏi của tôi ... Nhưng tôi sẽ giải thích vấn đề của tôi là chính xác, và cách Doug giúp tôi giải quyết vấn đề mà bạn có thể gặp phải.
Tôi gọi jquery $.post
và đang thay thế div của mình bằng chế độ xem một phần, như vậy.
function AddNewAddress (paramvalue) {
$.post(url, { param: paramvalue}, function(d) {
$('#myDiv').replaceWith(d);
});
}
Khi làm như vậy, vì một lý do khi bước vào mô hình của tôi giá trị được lựa chọn của tôi sở hữu liên kết không bao giờ được thiết lập, chỉ cho đến khi tôi bước vào xem nó được đưa vào phạm vi.
Vì vậy, những gì tôi đã trước
@Html.DropDownListUnobtrusiveFor(model => model.CustomerAddresses[i].YearsAtAddress, Model.CustomerAddresses[i].YearsAtAddressSelectList, new {onchange = "return Address.AddNewAddress(this,'" + @Url.Action("AddNewAddress", "Address") + "'," + i + ")"})
Tuy nhiên mặc dù Model.CustomerAddresses [i] .YearsAtAddressSelectList, được thành lập ... nó không thiết lập giá trị được chọn.
Vì vậy, sau ....
@Html.DropDownListUnobtrusiveFor(model => model.CustomerAddresses[i].YearsAtAddress, new SelectList(Model.CustomerAddresses[i].YearsAtAddressSelectList, "Value", "Text", Model.CustomerAddresses[i].YearsAtAddress), new { onchange = "return Address.AddNewAddress(this,'" + @Url.Action("AddNewAddress", "Address") + "'," + i + ")" })
và nó làm việc!
tôi quyết định không sử dụng DropDownListFor
vì nó có vấn đề khi sử dụng xác nhận không phô trương, đó là lý do tôi tham khảo những điều sau nếu bạn tò mò trong một lớp học xếp vào loại
HtmlExtensions.cs
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString DropDownListUnobtrusiveFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
{
return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, null /* optionLabel */, null /* htmlAttributes */);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString DropDownListUnobtrusiveFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes)
{
return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, null /* optionLabel */, new RouteValueDictionary(htmlAttributes));
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString DropDownListUnobtrusiveFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes)
{
return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, null /* optionLabel */, htmlAttributes);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString DropDownListUnobtrusiveFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel)
{
return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, optionLabel, null /* htmlAttributes */);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString DropDownListUnobtrusiveFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes)
{
return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, optionLabel, new RouteValueDictionary(htmlAttributes));
}
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Users cannot use anonymous methods with the LambdaExpression type")]
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString DropDownListUnobtrusiveFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes)
{
if (expression == null)
{
throw new ArgumentNullException("expression");
}
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
IDictionary<string, object> validationAttributes = htmlHelper
.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);
if (htmlAttributes == null)
htmlAttributes = validationAttributes;
else
htmlAttributes = htmlAttributes.Concat(validationAttributes).ToDictionary(k => k.Key, v => v.Value);
return SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel, htmlAttributes);
}
Xem DDL tôi hướng dẫn http: //www.asp. net/mvc/hướng dẫn/javascript/làm việc-với-the-dropdownlist-box-và-jquery/sử dụng-the-dropdownlist-helper-với-aspnet-mvc và http://blogs.msdn.com/b/rickandy/ lưu trữ/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT
Tôi tìm thấy một sửa chữa cho vấn đề này được báo cáo [ở đây] [1]. [1]: http://stackoverflow.com/a/11705380/277900 – ararog
Đi vào cùng một câu hỏi.Cuối ngày tôi chỉ làm một NẾU Razor và nếu bị vô hiệu hóa có một dòng với đó bị vô hiệu hóa mã hóa khác cùng một dòng sao chép nhưng không có mã hóa vô hiệu hóa. Nó không ngắn gọn - nhưng nó dễ đọc và làm theo cho devs khác. – ppumkin