Я испробовал каждое решение, но мой список пуст.
Можете ли вы указать мне правильное направление, я действительно не знаю, в чем проблема ... Просмотр:
@using (Html.BeginForm("EditProfile", "User", FormMethod.Post, new { @class = "form form-horizontal" }))
{
<div class="form-actions pb-0" />
for (int i = 0; i < Model.UserAddress.Count; ++i)
{
<input id="btc-limit-buy-total" hidden type="text" class="form-control" value="@Model.UserAddress[i].AddressId" name="ProfileViewModel.UserAddress[@i].AddressId">
<div class="form-group row">
<label class="col-md-3 col-form-label" for="btc-limit-buy-total">Shipping address:</label>
<div class="col-md-9">
<input id="btc-limit-buy-total" type="text" @(Model.EditMode ? "" : "disabled") value="@Model.UserAddress[i].AddressLine1@(Model.UserAddress[i].AddressLine2 != null ? $", {Model.UserAddress[i].AddressLine2}" : "")" class="form-control" name="ProfileViewModel.UserAddress[@i].AddressLine1">
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label" for="btc-limit-buy-total">City:</label>
<div class="col-md-9">
<input id="btc-limit-buy-total" type="text" @(Model.EditMode ? "" : "disabled") value="@Model.UserAddress[i].City" class="form-control" name="ProfileViewModel.UserAddress[@i].City">
</div>
</div>
<div class="form-group row" @(Model.UserAddress[i].StateProvinceRegion != null ? "" : "hidden")>
<label class="col-md-3 col-form-label" for="btc-limit-buy-total">State:</label>
<div class="col-md-9">
<input id="btc-limit-buy-total" type="text" @(Model.EditMode ? "" : "disabled") value="@Model.UserAddress[i].StateProvinceRegion" class="form-control" name="ProfileViewModel.UserAddress[@i].StateProvinceRegion">
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label" for="btc-limit-buy-total">Country:</label>
<div class="col-md-9">
<input id="btc-limit-buy-total" type="text" @(Model.EditMode ? "" : "disabled") value="@Model.UserAddress[i].Country" class="form-control" name="ProfileViewModel.UserAddress[@i].Country">
</div>
</div>
<div class="form-group row" @(Model.UserAddress[i].Zip != null ? "" : "hidden")>
<label class="col-md-3 col-form-label" for="btc-limit-buy-total">Postal code:</label>
<div class="col-md-9">
<input id="btc-limit-buy-total" type="text" @(Model.EditMode ? "" : "disabled") value="@Model.UserAddress[i].Zip" class="form-control" name="ProfileViewModel.UserAddress[@i].Zip">
</div>
</div>
<div class="form-group row" @(Model.UserAddress[i].DeliveryInstructions != null ? "" : "hidden")>
<label class="col-md-3 col-form-label" for="btc-limit-buy-total">Additional info:</label>
<div class="col-md-9">
<input id="btc-limit-buy-total" type="text" @(Model.EditMode ? "" : "disabled") value="@Model.UserAddress[i].DeliveryInstructions" class="form-control" name="ProfileViewModel.UserAddress[@i].DeliveryInstructions">
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label" for="btc-limit-buy-total">Phone number:</label>
<div class="col-md-9">
<input id="btc-limit-buy-total" type="text" @(Model.EditMode ? "" : "disabled") value="@Model.UserAddress[i].PhoneNumber" class="form-control" name="ProfileViewModel.UserAddress[@i].PhoneNumber">
</div>
</div>
<div class="form-actions pb-0" />
}
}
<input type="submit" value="Edit profile" class="btn round btn-success btn-block btn-glow" />
Я читал о публикации атрибута "name" в нужное значение, и ясделал это с префиксом ViewModel и без него, а список все равно null.Кроме того, я прочитал, что вы не можете использовать foreach, поэтому я использую для.Контроллер:
[HttpPost]
public ActionResult EditProfile(List<UserAddress> userAddresses)
{
var model = PopulateProfileViewModel();
model.EditMode = true;
if (!ModelState.IsValid)
{
return View("Profile");
}
//model.UserAddress = userAddresses;
//TODO update db
return View("Profile", model);
}
РЕДАКТИРОВАТЬ:
Класс UserAddress:
public class UserAddress
{
public short AddressId { get; set; }
public string FullName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string StateProvinceRegion { get; set; }
public string Zip { get; set; }
public string PhoneNumber { get; set; }
public string DeliveryInstructions { get; set; }
public bool Active { get; set; }
}