【问题标题】:load partial view by using jquery使用 jquery 加载部分视图
【发布时间】:2015-01-06 07:47:49
【问题描述】:

我想在 div 中显示部分视图。我尝试了很多方法,但没有得到结果。

请给我一个合适的解决方案,我是 mvc 的新手

这是主****索引页面****

@model TelerikAdmin.Models.ViewModel.UserViewModel

        @{
            ViewBag.Title = "Index";
           // Layout = "~/Views/Shared/_LayoutPage1.cshtml";
        }
        <script src="~/Scripts/jquery-1.7.1.js"></script>
        <script src="~/Scripts/jquery-1.7.1.min.js"></script>
        <style>
            .k-ff input.k-textbox {
                height: 2.17em;
                width:350px;

            }
            .content-wrapper {
                margin:5% auto auto 25%;
                border:1px solid yellow;
                width:550px;
            }
            #container tr td{
                padding:10px;
            }
        </style>
        <script>
            $(function () {
                $('#loadFromMainFrame').click(function (e) {
                    e.preventdefault();
                    var url = $(this).data(url);
                    $('#mainframeContens').load(url);
                });
            });
        </script>
        <input id="loadFromMainFrame" type="button" data-url="@Url.Action("GetView","Privilege")" value="Click" />
        <div id="mainframeContens">
        </div>

这是我的部分视图(patial.cshtml) 我将部分视图放在共享文件夹或任何其他地方

这是局部视图

这是我的控制器操作

using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using TelerikMvcApp5.Models.Context;
using TelerikAdmin.Models.ViewModel;

namespace TelerikAdmin.Controllers
{
    public class PrivilegeController : Controller
    {
        ShoppingContext db = new ShoppingContext();
        public ActionResult Index()
        {
         return View();
        }

        public ActionResult GetView()
        {
            return PartialView("partial");
        }

    }
}

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller


    【解决方案1】:

    请像下面这样使用。

    如下更改您的 JS。

    <script>
        $(function () {
            $('#loadFromMainFrame').click(function (e) {
                e.preventdefault();
    
                var url = $(this).data(url);
    
                $.ajax({
                    url: url,
                    type: 'GET',
                    success: function (result) {
                        $('#mainframeContens').html(result);
                    }
                });
            });
        });
    </script>
    

    注意:您的 Partial View 加载 Div 应该如下所示。

    <div id="mainframeContens">
        @Html.Partial("partial")
    </div>
    

    在您的 Action 方法中提及 HTTPGET,如下所示

    [HttpGet]
    public ActionResult GetView()
    {
        return PartialView("partial");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-16
      • 2011-03-31
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      相关资源
      最近更新 更多