简体   繁体   English

在Html.ActionLink上的Modal中使用表单呈现部分视图

[英]Render partial view with form in Modal on Html.ActionLink click

I have a page with table where each row has a link "Move". 我有一个带有表格的页面,其中每一行都有一个链接“ Move”。

On clicking of the link I am trying to get the controller GET method called which will in turn render the _MoveReportPartial view in a modal. 在单击链接时,我尝试获取调用的GET方法,该方法将依次以模态呈现_MoveReportPartial视图。

Once the user makes the selections in the modal the submit button should post to the Post method of the controller. 用户在模式中进行选择后,提交按钮应发布到控制器的Post方法中。

If I remove the class attribute (move-modal) from Html.ActionLink(...) , it in effect disengages the js file and ignores it. 如果我从Html.ActionLink(...)删除class属性(move-modal Html.ActionLink(...) ,则实际上会脱离js文件并忽略它。 Then it works by opening the _MoveReportPartial in a new window and then consequently posting to the correct method if user clicks submit. 然后,它通过在新窗口中打开_MoveReportPartial并随后在用户单击“提交”的情况下发布到正确的方法来工作。

I am trying to get it to open in the modal, but the js I have doesn't work and routes to the POST method instead on "Move" click. 我试图使其在模式中打开,但是我拥有的js无法正常工作,而是在“移动”单击时路由到POST方法。

EDIT Why does the .load call the POST method instead of the GET ? 编辑为什么.load调用POST方法而不是GET How can I change the js ? 如何更改js (added event.preventDefault(); , still the same behavior is observed) (添加了event.preventDefault();仍然观察到相同的行为)

The move link on the originating view looks like this: 原始视图上的移动链接如下所示:

<div class="d20 actionLink">
    @Html.ActionLink("Move", "MoveReport", "ReportsWeb", new {id = item.ReportDefinitionId, newReport = false}, new {@class = "move-modal"})
</div>

I have a js file: 我有一个js文件:

$(function () {
$('.move-modal').click(function () {
    event.preventDefault();
    $('<div/>').appendTo('body').dialog({
        close: function (event, ui) {
            dialog.remove();
        },
        modal: true
    }).load(this.href, {});
});

}); });

My ReportsWebController looks like this: 我的ReportsWebController看起来像这样:

[HttpGet]
public ActionResult MoveReport(Guid id, bool newReport)
{
    //some code

    return PartialView("_MoveReportPartial", model);
}

[HttpPost]
public ActionResult MoveReport(MoveReportModel Model)
{
    try
    {
        //some code
    }
    catch (Exception exc)
    {
        InternetReportingTrace.Source.WriteError(exc);
        throw;
    }
    return RedirectToAction("ListReports");
}

and my _MoveReportPartial looks like this: 我的_MoveReportPartial看起来像这样:

<div id="dialog-confirm">
<div align="center">
    <h2>Please Confirm</h2>        
</div>

@using (Html.BeginForm("MoveReport", "ReportsWeb", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div tabindex="-1" role="dialog">

        <div class="modal-content">
            <p>Report @Model.Report.Name with ID @Model.Report.ReportDefinitionId </p>
            <p>Will be moved to:</p>

            @for (int i = 0; i < Model.MoveOptions.Count; i++)
            {
                <div class="radio">
                    <label><input type="radio" name="optradio">@Model.MoveOptions[i]</label>
                </div>
            }
            <div>
                <input type="submit" value="Move Report" />
            </div>
        </div>
    </div>
}

I don't think you're preventing the default behaviour of the ActionLink properly. 我认为您没有正确阻止ActionLink的默认行为。

Try: 尝试:

$('.move-modal').click(function (event) {
  event.preventDefault();
  $('<div/>').appendTo('body').dialog({
    close: function (event, ui) {
        dialog.remove();
    },
    modal: true
  }).load(this.href, {});
});

Because it's a jQuery handler, you can use event.preventDefault() instead of return false; 因为它是一个jQuery处理程序,所以可以使用event.preventDefault()代替return false; event.preventDefault() . If your click handler uses return false to prevent browser navigation, it opens the possibility that the interpreter will not reach the return statement and the browser will proceed to execute the anchor tag's default behavior before that point. 如果您的点击处理程序使用return false阻止浏览器导航,则可能会导致解释器无法到达return语句,并且浏览器将在此之前继续执行锚标记的默认行为。 Using event.preventDefault() as the first line in the handler means you can guarantee that the default navigation behavior will not fire. 在处理程序的第一行中使用event.preventDefault()意味着您可以保证不会触发默认导航行为。

Secondly, your .load method call is wrong. 其次,您的.load方法调用是错误的。

Change 更改

.load(this.href, {}); 

to

.load(this.href);

The documentation at api.jquery.com/load states "The POST method is used if data is provided as an object; otherwise, GET is assumed." api.jquery.com/load上的文档指出:“如果将数据作为对象提供,则使用POST方法;否则,假定为GET。” Because you're sending it an empty object, it assumes you want to use POST. 因为您要向其发送一个空对象,所以它假定您要使用POST。 There's no need to do this. 不需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM