MVC 5 Ajax.BeginForm POST不绑定到控制器中的模型

时间:2014-08-04 08:38:27

标签: asp.net-mvc-5 ajax.beginform

对于这里的代码数量感到抱歉,但这是解释内容的最佳方式。

我在MVC 5局部视图中提供了此代码:

@model Hide.MVC.MFC.Models.RequestDataModel
using (Ajax.BeginForm("RequestSetCanceledState", "Home", null,
new AjaxOptions
{
    HttpMethod = "POST",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "request-info",
    OnBegin = "AjaxAbsoluteLoaderOn",
    OnComplete = "AjaxAbsoluteLoaderOff"
}, new { id = "formCancel" }))
{   
    <input id="sbmtCancel" type="submit" value="Cancel" />
    @Html.DropDownListFor(m => m.CancelReason, new SelectList(Model.CancelReasons, "Id", "Name", Model.CancelReason), String.Empty)
    @Html.TextBoxFor(m => m.Reason)
    @Html.HiddenFor(m => m.RequestId)
}

我的控制器操作如下:

[HttpPost]
public PartialViewResult RequestSetCanceledState(RequestDataModel model)
{
    ...
    return PartialView("....", model);
}

我的模型如下:

public class RequestDataModel
{   
    public RequestDataModel() { }

    public RequestDataModel(int requestId)
    {
        this.RequestId = requestId;
        using (var service = new InnerServiceClient())
        {
            var request = service.GetRequest(requestId);
            this.State = request.ServiceState;
            if (request.PremiseInformationId.HasValue)
            {
                this.PremisInformation = service.GetExistingData(requestId) ?? new PremiseInformationBL();
            }
        }
    }

    public int RequestId { get; set; }
    public int State { get; set; }

    public PremiseInformationBL PremisInformation { get; set; }
    public int? CancelReason { get; set; }
    public string Reason { get; set; }
    public List<ListItem> CancelReasons
    {
        get
        {
            using (var service = new DictionaryServiceClient())
            {
                return service.GetShortList(TypeDictionary.MFCCancelReason).ToList();
            }
        }
    }
}

代码发布到RequestSetCanceledState方法,浏览器发送消息

CancelReason=1&Reason=123&RequestId=48&X-Requested-With=XMLHttpRequest

但模型总是空的。另外,如果我使用 GET 请求,则模型不为空!我试图从模型中删除PremisInformation和CancelReasons,但它没有帮助。有人可以告诉我为什么会这样吗?

0 个答案:

没有答案