在MVC3中的Ajax.Beginform中调用自定义确认对话框

gar*_*lur 10 ajax asp.net-mvc jquery razor asp.net-mvc-3

我希望能够使用自定义jquery对话框,或者至少能够在Ajax.Beginform函数中使用AjaxOptions.Confirm属性时将按钮的文本从OK/Cancel更改为其他内容.像这样:

<div>
    @using (Ajax.BeginForm("Function", "Controller", new { id = theId }, new AjaxOptions
        {
            HttpMethod = "POST",
            UpdateTargetId = "theForm",
            InsertionMode = InsertionMode.Replace,
            LoadingElementId = "iconGif",
            OnBegin = "OnBegin",
            OnFailure = "OnFailure",
            OnSuccess = "OnSuccess",
            Confirm = "Are you sure?" //TODO: Confirm with different dialog?
        }, new { id = "feedback-form" }))
    {
        //Some stuff
        <button onclick="derp()">Submit</button>
    }
</div>
Run Code Online (Sandbox Code Playgroud)

有没有办法通过AjaxOptions.Confirm属性使用Ajax.Beginform实现这一点?

Iva*_*ago 6

我遇到了这个来自定义 AjaxOptions Confirm 文本,该值在呈现 Ajax.Beginform 时尚未创建
例如:
Confirm="Are you sure you want to create Customer Id" + someValue + "?"

终于找到了一个解决方案:该方法是关于使用 JQuery 更改提交按钮行为以提取值,运行我自己的确认对话框并在用户确认时提交 Ajax 表单。

步骤:
1-从 AjaxOptions 中删除Confirm避免设置按钮的 type="submit",可能是 type="button"

<div>
    @using (Ajax.BeginForm("Function", "Controller", new AjaxOptions
        {
            HttpMethod = "POST",
            UpdateTargetId = "theForm",
            InsertionMode = InsertionMode.Replace,
            LoadingElementId = "iconGif",
            OnBegin = "OnBegin",
            OnFailure = "OnFailure",
            OnSuccess = "OnSuccess"
            // Confirm option has been removed 
        }, new { id = "feedback-form" }))
    {
        //Some stuff
        <button id="buttonId" type="button" onclick="derp()">Submit</button> //Setting id and type
    }
</div>
Run Code Online (Sandbox Code Playgroud)

2- 在页面或布局中引用的 js 文件中添加以下内容。

$("#buttonId").click(function () { 
if(confirm("Are you sure you want to create Id "+$("#CustomerId").val() + " ?"))
{
$("#feedback-form").submit(); // Submitting the form if user clicks OK
    }
    });
Run Code Online (Sandbox Code Playgroud)

'CustomerId' 是页面中某些隐藏输入的 ID。
我希望这有帮助。


Dar*_*rov 4

不,您无法使用 AjaxOptions 的Confirm 属性来实现此目的。这仅使用window.confirmjavascript 方法,该方法不允许任何 UI 自定义并且依赖于浏览器。您必须自己实现此功能。例如,您可能想签出jQuery UI dialog plugin.