作为模型的Ajax.BeginForm路由值未更新(值)

时间:2012-11-19 07:46:50

标签: asp.net-mvc asp.net-mvc-3 razor ajax.beginform

using (Ajax.BeginForm("SaveTimeShift", new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "recordList", InsertionMode = InsertionMode.Replace }))

为什么当我使用Ajax.BeginForm的这个重载构造函数时,我得到了 更新我的模型中的数据,但当我在下面使用这个重载的构造函数时,我没有得到我的模型的更新值?我需要下面的构造函数,以便我设置我的表单的html类属性...

  using (Ajax.BeginForm("SaveTimeShift", @Model, new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "recordList", InsertionMode = InsertionMode.Replace }, new { @class = "form-inline" }))
            {


        @Html.TextBoxFor(model => model.StartDate, new { id = "startDate", @readonly = true, width = "100px" })
        @Html.HiddenFor(model => model.SelectedName, new { id = "selectedName" });
        @Html.HiddenFor(model => model.SelectedUserId, new { id = "selectedUserId" });



        <input class="btn btn-primary pull-right" type="submit" value="Save Time Shift" />
        }

1 个答案:

答案 0 :(得分:1)

第二个Ajax.BeginForm()具有以下方法签名:

public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, Object routeValues, AjaxOptions ajaxOptions, Object htmlAttributes)

您尝试将@Model作为路由值传递,而不是模型绑定的工作方式。模型绑定器将通过发布的表单值构建复杂的模型对象。将Ajax.BeginForm()方法更改为以下内容可以解决您的问题:

@using (Ajax.BeginForm("SaveTimeShift", new { }, new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "recordList", InsertionMode = InsertionMode.Replace }, new { @class = "form-inline" }))
{
    // Your textboxes, etc corresponding to your model properties go here
}