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

时间:2018-11-29 13:07:55

标签: c# html .net asp.net-mvc html-helper

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

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

@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" />

我读到有关将“名称”属性发布到正确的值的信息,并在有和没有ViewModel前缀的情况下做到了这一点,并且list仍然为null。另外,我读到你不能使用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);
        }

编辑:

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

1 个答案:

答案 0 :(得分:1)

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