为什么模型没有被控制器动作接收?

时间:2014-04-04 09:46:57

标签: asp.net-mvc-4 razor asp.net-mvc-5

剃刀代码:

 @model Clarifi.API.DataContract.ManualScanItem
    @using (Html.BeginForm(new { @class = "pull-left" }))
    { 
        @Html.HiddenFor(m => m.ItemType)
        @Html.HiddenFor(m => m.ItemIdentifier)
        @Html.HiddenFor(m => m.Hotel.Address.AddressLine1)
        @Html.HiddenFor(m => m.Hotel.Address.AddressLine2)
        @Html.HiddenFor(m => m.Hotel.Address.CityCode)
        @Html.HiddenFor(m => m.Hotel.Address.CityName)
        @Html.HiddenFor(m => m.Hotel.Address.CompleteAddress)
        @Html.HiddenFor(m => m.Hotel.Address.CountryCode)
        @Html.HiddenFor(m => m.Hotel.Address.StateCode)
        @Html.HiddenFor(m => m.Hotel.Address.StateName)
        @Html.HiddenFor(m => m.Hotel.Address.ZipCode)
        @Html.HiddenFor(m => m.Hotel.ClarifiId)
        @Html.HiddenFor(m => m.Hotel.GeoCode.Latitude)
        @Html.HiddenFor(m => m.Hotel.GeoCode.Longitude)
        @Html.HiddenFor(m => m.Hotel.GeoCode.GeoCodeSource)
        @Html.HiddenFor(m => m.Hotel.HasIssue)
        @Html.HiddenFor(m => m.Hotel.Name)
        @Html.HiddenFor(m => m.Hotel.PhoneNumber)
        @Html.HiddenFor(m => m.Hotel.Rating)
        @Html.HiddenFor(m => m.Hotel.SupplierFamily)
        @Html.HiddenFor(m => m.Hotel.SupplierId)

        <input type="submit" value="Verify" class="button-orange btn-action mtm btn-validate">
    }

控制器中的动作:

public ActionResult MapGeoCode(ManualScanItem hotelModel)
        {
            ItemRs<ManualScanItem> response = manualScanProvider.GetNextScanItem(ManualScanItemType.GeoCodeScan);

            if (!response.IsSuccess)
            {
                //this.SetErrorView(response);
                return View("ErrorView");
            }

            return View(response.Item);
        }

我甚至检查了http请求。还有输入数据。但在我的控制器动作中,'hotelModel'为空。该怎么办?为什么会这样呢?

3 个答案:

答案 0 :(得分:0)

您将模型传递到某个地方的视图?类似的东西:

    [HttpGet]
    public ActionResult MapGeoCode()
    {
        return View(new ManualScanItem());
    }

    [HttpPost]
    public ActionResult MapGeoCode(ManualScanItem hotelModel)
    {
        ItemRs<ManualScanItem> response = manualScanProvider.GetNextScanItem(ManualScanItemType.GeoCodeScan);

        if (!response.IsSuccess)
        {
            //this.SetErrorView(response);
            return View("ErrorView");
        }

        return View(response.Item);
    }

答案 1 :(得分:0)

[HttpGet]
public ActionResult MapGeoCode()
{
    return View();
}

[HttpPost]
public ActionResult MapGeoCode(ManualScanItem hotelModel)
{
    ItemRs<ManualScanItem> response = manualScanProvider.GetNextScanItem(ManualScanItemType.GeoCodeScan);

    if (!response.IsSuccess)
    {
        return View("ErrorView");
        //or return get action
        return View(response.Item);
    }

    return RedirectToAction("Index");
}

在视图中尝试这个:

@using (Html.BeginForm("MapGeoCode", "Controller", FormMethod.Post, new { @class = "pull-left" }))
{
  //...
}

答案 2 :(得分:0)

使用此重载..

@using (Html.BeginForm("actionname", "controllername", FormMethod.Post, new { @class = "your class name" })) { 

}