简体   繁体   中英

@Url.Action returns a null value within the jQuery .load() method

I'm very new to MVC so please keep the explanations simple and specific. I essentially am trying to open a modal dialog using jQuery and @Url.Action keeps returning a null value. When I forego @Url.Action and hardcode the path, it works fine. Here's the code below.

Script:

<script type="text/javascript">
        function popupMobileDialog() {
            $('#dialog').dialog({
                autoOpen: false,
                width: 700,
                height: 500,
                resizable: false,
                title: 'CURewards Mobile Apps',
                modal: true,
                open: function (event, ui) {
                    $(this).load('@Url.Action("MobilePopup", "MobilePopup", new { area = "Innerge.Plugins.PSCU"})');
                    //$(this).load('MobilePopup/MobilePopup');
                },
                buttons: {
                    "Ok": function () {
                        $(this).dialog("close");
                    }
                }
            }).dialog("open");
            }
</script>

Controller:

[Export(typeof(IController))]
[ExportMetadata("Name", "MobilePopup")]
[ExportMetadata("Plugin", true)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MobilePopupController : Controller
{
    public ActionResult MobilePopup()
    {
        return View();
    }

}

View:

@{
    Layout = null;
}

<p style="text-align:center; font-weight:bold;">Yay!!!  This is the modal popup window I've been waiting for.</p>

Any ideas as to what I'm doing wrong, please let me know. If you need any more information, please let me know. Thanks!!!

The following line of code resolved my issue.

$(this).load('@Url.Action("MobilePopup", "MobilePopup", new { area = ""})');

My layout.cshtml file is located in /Views/Shared while MobilePopupController is located off of the root structure (/Controllers), in another area. So, by adding the "new { area = ""}" parameter, it tells ASP.NET that it'll find the controller in the root/default area of the project, which is the project structure outside of any areas folder. Without the area parameter, it tells ASP.NET to look in the current area for the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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