将对象列表从视图发送到操作

Sending list of objects from view to action

我尝试了所有解决方案,但我的列表仍然为空。

你能给我指明正确的方向吗,我真的不知道有什么问题... 查看:

@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 前缀的情况下都这样做了,但列表仍然为空。另外,我读到你不能使用 foreach,所以我正在使用 for。 控制器:

[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);
        }

编辑:

用户地址class:

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; }
    }

我让它在一个测试项目中工作。我更改了您的控制器方法以接受包含 UserAddress 对象列表的类型的 model,然后将所有 names 更改为 parameter name in controller .ListName[x].PropertyName