【问题标题】:why the model is not received by the controller action?为什么控制器动作没有接收到模型?
【发布时间】:2014-04-04 21:25:07
【问题描述】:

剃刀代码:

 @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”为空。该怎么办?为什么会这样?

【问题讨论】:

  • 方法上面有HttpGet和HttpPost注解吗? get方法的名字和MapGeoCode一样吗?
  • 我尝试了放 [HttpPost] 和不放...但没有任何效果

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


【解决方案1】:

您将模型传递给某处的视图?类似的东西:

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

【讨论】:

  • Yaa.. 它不工作不知道我错过了什么。无论如何,我在 FormCollection 的帮助下完成了它,然后将其转换为我的本地模型
【解决方案2】:
[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" }))
{
  //...
}

【讨论】:

    【解决方案3】:

    改用这个重载..

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 2018-06-15
      相关资源
      最近更新 更多