简体   繁体   English

为什么httpPost模型始终为null?

[英]Why httpPost model always null?

I have get and post controller. 我有获取和发布控制器。 But by httpPost comtroller passing model parameter values are null. 但是通过httpPost控制器传递的模型参数值为null。 Why my httpPost model parameter values always null ?? 为什么我的httpPost模型参数值始终为null?

[HttpGet]
        public ActionResult HireItem()
        {

            var HireItemListModel = new HireItemModel();

            HireItemListModel = new HireItemModel()
            {
                first_name = Session["first_name"].ToString(),
                middle_name = Session["middle_name"].ToString(),
                last_name = Session["last_name"].ToString(),
                ceremony_date = Session["ceremony_date"].ToString(),
            };         


            var product = _productService.GetAllHireProducts();

            if (product.Count != 0)
            {

                foreach (var proValue in product)
                {
                    var productVarSeparateList = _productService.GetHireProductVariantsByProductIds(proValue.Id, false);

                    foreach (var HireProSep in productVarSeparateList)
                    {
                        var productVarSeparateModel = new HireItemModel.HireItemSeparatetModel()
                        {
                            pname = HireProSep.Name,
                            price =HireProSep.Price,
                            pId=HireProSep.Id,

                        };
                        HireItemListModel.HireItemSeparatetlist.Add(productVarSeparateModel);
                    }
                    var productVarSetList = _productService.GetHireProductVariantsByProductIds(proValue.Id, true);

                    foreach (var HireProset in productVarSetList)
                    {
                        var productVarListset = new HireItemModel.HireItemSetModel()
                        {
                            pname = HireProset.Name,
                            price = HireProset.Price,
                            pId = HireProset.Id,
                        };
                        HireItemListModel.HireItemSetList.Add(productVarListset);
                    }
                }
            }

            return View(HireItemListModel);

        }

This controller HireItemModel model parameter values are null. 此控制器的HireItemModel模型参数值为null。 WHY?? 为什么??

[HttpPost,ActionName("HireItem")]
    public ActionResult HireItem(string submitB, FormCollection formCollection, HireItemModel HireItemListModel)
    {
        var graduandList = _graduandService.GetGraduandBynameCeremony(HireItemListModel.ceremony_id, HireItemListModel.first_name, HireItemListModel.middle_name, HireItemListModel.last_name);
        foreach (var graduand in graduandList)
        {
            graduand.height = HireItemListModel.height;
            graduand.head_circumference = HireItemListModel.head_circumferenc;
            _graduandService.Updategraduand(graduand);
        }

this is my view. 这是我的看法。

@model HireItemModel
    @using (Html.BeginForm())
     { 

         <table  >

     <tr>
        <td >
          Ceremony : 
        </td>
        <td>
           Ceremony at @Model.ceremony_date

        </td>
    </tr>

      <tr>
                <td >
                  Name :
                </td>
                <td >
                   @Model.first_name  @Model.middle_name  @Model.last_name
                </td>
            </tr>
            </table>
         <div id="HItemType_1">
         @Html.CheckBox("HItemType")
       @*<input type="checkbox" name="test" value="test" id="HItemType"  />*@
         <label> Academic Dress Set</label>

         </div>
     <div id="HsetItem">


                @Html.Partial("_LoadHireSetItem", @Model.HireItemSetList)
      </div> 


          <div  id="HseparateItem">
                @Html.Partial("_LoadHireSeparateItem", @Model.HireItemSeparatetlist)
           </div>


         <table  >
         <tr>
         <td colspan="2">
         Please tell us your measurement:
         </td>
         </tr>
     <tr>
        <td >
        Height (in cm):
        </td>
        <td>
         @Html.EditorFor(model => model.height)


        </td>
    </tr>

      <tr>
                <td >
                 Head circumference (in cm):
                </td>
                <td >
                 @Html.EditorFor(model => model.head_circumferenc)

                </td>
            </tr>
            </table>

         <div>
        <input class="productlistaddtocartbutton" type="submit"  value="Add to cart" name="submitB"  id="btnaddtocart"/>
         </div>

     }

thanks. 谢谢。

Make sure inside your view you have input fields for all values that you intend to use in your POST action. 确保在视图内部,您具有要在POST操作中使用的所有值的输入字段。 For example if you want to use the ceremony_id , first_name , middle_name , and last_name properties you need to include them: 例如,如果你想使用ceremony_idfirst_namemiddle_namelast_name你需要包括它们的属性:

@Html.HiddenFor(model => model.ceremony_id)
@Html.HiddenFor(model => model.first_name)
@Html.HiddenFor(model => model.middle_name)
@Html.HiddenFor(model => model.last_name)

You could use hidden fields if the user is not supposed to modify their values but you could also have used text fields depending on your requirements. 如果不希望用户修改其值,则可以使用隐藏字段,但也可以根据需要使用文本字段。

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

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