Create a popup that renders a partial view

不打扰是莪最后的温柔 提交于 2019-12-08 08:57:37

问题


I've been struggling with a popup for the last few days. Here's a little information. I am very new to using JavaScript, but I'm eager to learn. First of all here is an example of what I want to make. (Press 'Inloggen' on the top right corner to make it pop up) The CSS is no problem. I got this working using bPopUp and copying editing the code used in the site, not the examples.

But anyway, the site demonstrated here is made using php. But my boss decided to switch to MVC 4 .NET. Which isn't really a problem, except for the fact that it doesn't work anymore. And I don't know why.

So I decided to start over, but I could use a little help.

What I would prefer to achieve is to have one <div> that can be re used and renders a partial view.

To trigger it I would like something in the lines of:

       <span class="popup_button white" data-popup="Some/view" data-height='200'>Inloggen</span>

This may be other tags instead of span and Id can be used instead of class to define it's a pop-up trigger. (Any entire other solutions are welcome as well of course). I would like the use the data-popup attribute to specify which partial view to render when triggered and the data-height attribute to specify the height of the content of the popup.

Can anyone one give me tips on how to reach this? I know it might be much to ask, but every suggestion is welcome. Due to my inexperience with JavaScript I prefer code examples, but any architectural suggestions will be happily accepted.

Thank you guys in advance :D


回答1:


$('#my-button').click(function () { 
    var BaseUrl = "/Popup/" + $('#my-button').data("popup"); 
    $.ajax({
        url: BaseUrl,
        type: "POST",
        data: { Id: $('.Id').val() }
        cache: false,
        async: true,
        success: function (result) {
            $(".content").html(result);
            //There are different ways to do the pop up.  you mentioned you had the popup working.  trigger it here
            $('#popup').fadeIn("slow"); 
            $('.fader').fadeIn("slow"); 
        }
    });
});

Then in your controller

[HttpPost]
public PartialViewResult Action(int Id){
    //populate a model
    return PartialView("_PartialViewName", model);
}

Let me know if you have any more questions.



来源:https://stackoverflow.com/questions/19897157/create-a-popup-that-renders-a-partial-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!