简体   繁体   English

复杂对象的隐藏字段未传递给控制器

[英]Hidden fields of complex object not passing to controller

I am using the HTML helper HiddenFor inside of a for loop to bind some properties of a complex object back to my model. 我在for循环中使用HTML帮助器HiddenFor将复杂对象的某些属性绑定回我的模型。

If I interrogate the view, I can see the values are being set correct in the hidden fields, but when I submit my form, the values are null in the controller. 如果查询视图,则可以在隐藏字段中看到正确设置的值,但是当我提交表单时,控制器中的值为null。

VIEW 视图

 @for (int i = 0; i < Model.CustomerSearchResponses.customers.Count; i++)
 {
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].name.first)
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].name.last)
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].name.company)
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].address.line1)
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].address.line2)
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].address.city)
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].address.stateProvince)
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].address.postalCode)
     @Html.HiddenFor(m => m.CustomerSearchResponses.customers[i].address.country)
  }

<input id="CustomerSearchResponses_customers_0__name_first" name="CustomerSearchResponses.customers[0].name.first" type="hidden" value="BRIAN">

MODEL 模型

    public class RequestViewModel
{
    **some other props**
    public CustomerSearchResponse CustomerSearchResponses { get; set; }
}

CONTROLLER 控制器

[HttpPost]
public ActionResult QASAddress(RequestViewModel model, string previousButton, string submitButton)
{
// Do Stuff 
}

Hidden Fields 隐藏的领域 在此处输入图片说明

Not all hidden fields are posted here for the sake of length, but they are all setting correctly. 出于篇幅考虑,并非所有隐藏字段都张贴在此处,但是它们都设置正确。 When I POST my form, the CustomerSearchResponses.customers[i] object is there, but the name and address objects are null. 发布表单时,出现了CustomerSearchResponses.customers[i]对象,但是nameaddress对象为空。

I created a sample application with GET and POST actions in a controller and I see customer properties in POST as shown below: 我在控制器中使用GET和POST操作创建了一个示例应用程序,并在POST中看到了客户属性,如下所示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM