如何将模型从视图传递到Controller的HttpPost方法?

时间:2016-07-05 18:41:11

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

因此,我有一个显示人员地址的表单(大多数人都有多个地址),表单显示列出的当前地址,并允许他们更新城市名称。单击“保存”按钮时,模型不会返回。其中一些必须进行修改,以便逻辑可以简化,如果它似乎关闭,提前对不起。当我保存HttpPost actionresult方法时,将模型返回为null。我做错了什么?

控制器:

[HttpGet]
    public ActionResult CANBudgets(int addressId)
    {
        PersonRepository.PersonDetailsModel model = new PersonRepository.PersonDetailsModel();
        model.Addresses= model.Addresses.Where(a => a.Address_Id == addressId).ToList();

        return PartialView(model);
    }

    [HttpPost]
    public ActionResult Address(PersonModel.PersonDetailsModel pplModel)
    {
        return PartialView(pplModel);
    }

型号:

    public class PersonDetailsModel : Person
    {
        public int? Person_Id { get; set; }
        public string Person_First_Name { get; set; }
        public string Person_Last_Name { get; set; }
        public List<AddressDetails> Addresses
        {
            get
            {
                return _entities.SelectAddresses(Person_ID).Select(a => new AddressDetails
                {
                    Address _Id = a.Address _Id
                     ,
                    Address = a.Address 
                     ,
                    State = a.State 
                     ,
                    City = a.City 
                     ,
                    Zip_Code = a.Zip_Code 
                }
                ).ToList();
            }

            set
            {

            }
        }
    }

查看

@using Personnel.ViewModel;
@using Personnel.Repository;
@model  PersonModel.PersonDetailsModel

    @using (Html.BeginForm("Address", "Person", FormMethod.Post, new { pplModel = Model }))
{
    <div class="row can-totals">
        <div class="table-responsive col-md-5">
            <table class="table table-bordered table-condensed table-responsive address-tbl" id="tblAddrSummary" summary="Address Summary" style="padding-left: 10px;">
                <thead>
                    <tr class="summary-header">
                        <th class="dt-head-center">#</th>
                        <th class="dt-head-center">Person Address</th>
                    </tr>
                </thead>
                <tbody>
                    @{
    int index = 0;
                    }

                    @foreach (var item in Model.Addresses)
                    {
                        <tr>
                            <td class="addr-number">@String.Format("{0} - {1}", @item.Address_Id, @item.Address)</td>


                            <td class="addr-city">@Html.TextBox("city", Model.Addresses[index].City, new { @class = "form-control" })</td>
                        </tr>
                        index++;
                    }
                </tbody>
            </table>
        </div>
    </div>
    <button type="submit" class="btn btn-primary">Save</button>

}

0 个答案:

没有答案