通过JavaScript更改MVC Ajax表单的InsertionMode?

时间:2014-10-18 11:35:53

标签: javascript c# ajax asp.net-mvc asp.net-mvc-ajax

我想在客户端通过javascript更改一些MVC Ajax表单参数,例如InsertionModeLoadingElementId
我该怎么办?

MVC ajax表单的样本:

@using (Ajax.BeginRouteForm("DevicesByObjectName", new AjaxOptions
{
    InsertionMode = InsertionMode.InsertBefore,
    UpdateTargetId = "Devices",
    LoadingElementId = "LoaderContents",
    OnSuccess = "MoreDevicesOnSuccess",
    OnFailure = "MoreDevicesOnFailure",
    OnBegin = "MoreDevicesOnBegin",
    OnComplete = "MoreDevicesOnComplete",
}))
{
    <div>
        @Html.AntiForgeryToken()
        <input type="hidden" value="@Model.Object.Id" id="ObjectId" name="ObjectId" />
        <input type="hidden" value="2" id="PageNumber" name="PageNumber" />
        <input type="hidden" value="" id="Filtering" name="Filtering" />

        <div class="center-block" style="max-width: 360px;">
            <input type="submit" value=" more" class="btn btn-primary btn-lg btn-block center-block" />
        </div>
        <div id="LoaderContents" class="ajax-loader center-block hidden"></div>
    </div>
}

1 个答案:

答案 0 :(得分:2)

BeginRouteForm根据<form>生成包含一系列data-xxx属性的AjaxOptions代码,例如

<form ... data-ajax-loading="#LoaderContents" data-ajax-mode="before" ...>

您可以使用jquery的.data().attr()方法获取和设置这些内容。所以对于你帮助者生成的表格

console.log($('form').data('ajax-mode')); // returns before
$('form').data('ajax-mode', 'after'); // change the insertion mode
console.log($('form').data('ajax-mode')); // returns after