Я пытаюсь опубликовать некоторый вложенный объект.
У меня есть класс Cart, и внутри есть два свойства ShippingAddress и BillingAddress.Адреса имеют тип ContactInformation.
В одном представлении я включаю частичное представление (AddressForm), которому я передаю ShipppingAddress, а в другом представлении я хочу показать то же частичное представление, но я передаю ему BillingAddress.
Теперь, когда я заполняю ShippingAddress, я передаю методу post весь объект Cart, но ShippingAddress пуст.
Я попытался настроить ViewData.TemplateInfo.HtmlFieldPrefix на основе типа адресаЯ прохожу там.Итак, когда я передаю ShippingAddress, HtmlFieldPrefix имеет значение «ShippingAddress».И все идентификаторы элементов действительно имеют префикс ShippingAddress.В методе post после отправки у меня есть эта [Bind (Prefix = "ShippingAddress")] модель CartViewModel, но все еще информация о доставке пуста.
Вот некоторый код
Модель корзины
public class CartViewModel
{
public ContactInformation ShippingAddress { get; set; } = new ContactInformation(EAddressType.ShippingAddress);
public ContactInformation BillingAddress { get; set; } = new ContactInformation(EAddressType.BillingAddress);
}
Модель адреса
public class ContactInformation
{
public EAddressType AddressType { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public ContactInformation(EAddressType addressType)
{
AddressType = addressType;
}
}
Вот мое представление, где я хочу отобразить форму адреса доставки
@model CartViewModel
@using (Ajax.BeginForm(InformationFormSubmit, Cart, null,
new AjaxOptions
{
HttpMethod = "POST",
AllowCache = false,
InsertionMode = InsertionMode.Replace,
UpdateTargetId = _InformationForm,
}, htmlAttributes: new { }))
{
<div id="_InformationForm">
@Html.Partial(_InformationForm, Model)
</div>
}
_InformationForm выглядит следующим образом
@model CartViewModel
@Html.Partial(_AddressForm, Model.ShippingAddress)
<button type="submit" >@Resource.ContinueToShipping</button>
И, наконец, форма моего адреса
@model ContactInformation
ViewData.TemplateInfo.HtmlFieldPrefix = Model.AddressType == EAddressType.ShippingAddress ? "ShippingAddress" : "BillingAddress";
@Html.HiddenFor(m => m.AddressType)
@Html.TextBoxFor(a => a.FirstName, htmlAttributes: new { id = Html.IdFor(a => a.FirstName)})
@Html.TextBoxFor(a => a.LastName, htmlAttributes: new {id = Html.IdFor(a => a.LastName) })
@Html.TextBoxFor(a => a.Address, htmlAttributes: new { id = Html.IdFor(a => a.Address)})
@Html.TextBoxFor(a => a.City, htmlAttributes: new { id = Html.IdFor(a => a.City)})
@Html.TextBoxFor(a => a.PostalCode, htmlAttributes: new {id = Html.IdFor(a => a.PostalCode) })
Мое почтовое действие выглядит следующим образом
[HttpPost]
public virtual ActionResult InformationFormSubmit([Bind(Prefix = "ShippingAddress")] CartViewModel model)
{
if (ModelState.IsValid)
{
CartViewModelEntity.ShippingAddress.FirstName = model.ShippingAddress.FirstName;
CartViewModelEntity.ShippingAddress.LastName = model.ShippingAddress.LastName;
CartViewModelEntity.ShippingAddress.Address = model.ShippingAddress.Address;
CartViewModelEntity.ShippingAddress.City = model.ShippingAddress.City;
CartViewModelEntity.ShippingAddress.PostalCode = model.ShippingAddress.PostalCode;
}
return PartialView(_InformationForm, model);
}
Но, конечно, адрес доставки пуст в почтовом действии ..
Не уверен, что я тут не так делаю ..
Спасибо за помощь