【问题标题】:Pop-up JSON showing partial显示部分的弹出 JSON
【发布时间】:2013-03-20 11:46:16
【问题描述】:

我想使用 Ajax 准备弹出窗口,但我不知道如何添加 Partial(我有错误)

问题

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

所有示例:

$("#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)';
                }
            }
        });
    });

【问题讨论】:

  • 你不能设置 window.location = '@Url.RenderPartial("DodajTemat", Model)' - 导致'@Url.RenderPartial("DodajTemat", Model)' 返回视图不是url地址
  • 当我写 @Html.RenderPartial("DodajTemat", Model)' 我得到信息 -> 表达式必须渲染值才能渲染
  • 什么是返回'@Url.RenderPartial("DodajTemat", Model)'?
  • 有错误,我发送我不知道如何实现部分。我检查了一些方法。
  • 尝试将@Html.RenderPartial("DodajTemat", Model)' 附加到某个div

标签: jquery asp.net-mvc asp.net-mvc-3 asp.net-mvc-2


【解决方案1】:

如果您想显示弹出窗口/灯箱/叠加层,那么我建议正确的方法是使用 HTML 和 CSS 来创建它。

就局部视图而言,我将通过 action 方法返回它。创建一个返回 this 的 AjaxResult 类。

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

那么你的 ajax 结果应该是这样的

$("#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);
            }
        }
    });
});

【讨论】:

    【解决方案2】:

    HTML

    点击按钮后,您可以在 HTML 中声明 Action 及其对应的 Controller 信息。您也可以将其设置为隐藏。

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

    JQuery

    在下面的代码中。

    1. 首先加载控件/弹出窗口。
    2. 获取控件/弹出窗口的信息

    $('#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) {
                }
            });
        });
    });
    

    如果你注意上面的代码,我正在获取两个Url。

    1. 用于加载的弹出/局部视图
    2. 对于模型并将模型信息传递给 PopUp。

    您应该从 Action Method 而不是从 javaScript 将模型信息发送到控件。

    部分视图加载的控制器操作

    //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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多