1

I would like to prepare pop-up using Ajax but I don`t know how to add Partial (there I have got error)

Problem

window.location = '@Url.RenderPartial("DodajTemat", Model)';

All example:

$("#dodajTemat_U").click(function () {
        $.ajax({
            url: '@Url.Action("DodajTemat", "StronaGlowna")', 
            dataType: "json",
            type: "POST",
            async: false,
            error: function () {
            },
            success: function (data) {
                if (data.Success) {
                    window.location = '@Url.RenderPartial("DodajTemat", Model)';
                }
            }
        });
    });
Rafał Developer
  • 2,135
  • 9
  • 40
  • 72

2 Answers2

3

If you would like to display a popup/lightbox/overlay then I would suggest the correct approach is to use HTML and CSS to create this.

In terms of the partial view I would have this returned by the action method. Create an AjaxResult class that returns this.

public class AjaxResult
{
    public string Html { get; set; }
    public bool Success { get; set; }
}

Then your ajax result would look like this

$("#dodajTemat_U").click(function () {
    $.ajax({
        url: '@Url.Action("DodajTemat", "StronaGlowna")', 
        dataType: "json",
        type: "POST",
        async: false,
        error: function () {
        },
        success: function (data) {
            if (data.Success) {
                $('body').append(data.Html);
            }
        }
    });
});
Colin Bacon
  • 15,436
  • 7
  • 52
  • 72
2

HTML

On your button click, you can declare the Action and it's corresponding Controller information in HTML. you can also set it hidden.

<div id="MyDiv" Control-Url = "@Url.Action("ActionName", "ControllerName")"
                Action-Url = "@Url.Action("ActionName", "ControllerName")">
</div>

JQuery

In the below code.

  1. First Load the Control/Pop up.
  2. Get the information for your control/Pop Up

$('#dodajTemat_U').click(function () {
    var MyDiv = $('#MyDiv');
    var url = MyDiv.attr('Control-Url');
    MyDiv.load(url, function () {
        url = MyDiv.attr('Action-Url');
        $.ajax({
            url: url,
            async: true,
            type: 'POST',
            beforeSend: function (xhr, opts) {
            },
            contentType: 'application/json; charset=utf-8',
            complete: function () { },
            success: function (data) {
            }
        });
    });
});

If you pay attention, to the above code, I am fetching two Url.

  1. For the Pop Up/Partial View for load
  2. For the Model and pass the Model information to PopUp.

You should send the Model information to the control from Action Method and not from the javaScript.

Controller Action for Partial View Load

//For loading the Pop-Up or for the Partial View
[HttpGet]
public PartialViewResult YourActionName()
{
    return PartialView("PartialViewName");
}

//For fetching the Model information and pass it to View.
public JsonResult ActionForGetInformation()
{
    return Json(Your Model Information goes here, JsonRequestBehavior.AllowGet);
}