【问题标题】:jquery load partial view asp.net mvcjquery加载部分视图asp.net mvc
【发布时间】:2011-03-31 03:10:35
【问题描述】:

我在使用母版页的基本 index.aspx 页面上有一些标题选项卡。

标题选项卡从 viewusercontrol () 正确呈现在索引页上。问题是我试图在没有任何运气的情况下将其他部分视图加载到索引页面中。有人能指出我做错了什么吗?

在母版页上我添加了以下 js 代码:

$(document).ready(function () {
    $('div.headertabs span').click(function () {
        var tabclass = $(this).attr('class')
        var tabid = $(this).children('a').attr('id')
        if (tabclass.indexOf("selected") == -1) {
            $(this).parent().children('.selected').removeClass('selected');
            $(this).addClass('selected');
            switch (tabid) {
                case "dashboard": $('#MainContent').load('<%= Html.RenderAction("ViewDashboard") %>');
                default: $('#MainContent').load('<%= Html.RenderAction("ViewDashboard") %>');
            }
        }
    });
});

HomeController.vb

Function ViewDashboard() As ActionResult
    Return PartialView()
End Function

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-mvc-2 partial-views


    【解决方案1】:
    【解决方案2】:

    另请参阅:This question 或 this one。我使用这个解决方案(也来自某处):

    public class OfferController : Controller
    {
        [HttpPost]
        public JsonResult EditForm(int Id)
        {
            var model = Mapper.Map<Offer, OfferEditModel>(_repo.GetOffer(Id));
    
            return Json(new { status = "ok", partial = this.RenderPartialViewToString("Edit", model) });
        }
    }
    
    
    
    public static partial class ControllerExtensions
    {
        public static string RenderPartialViewToString(this ControllerBase controller, string partialPath, object model)
        {
            if (string.IsNullOrEmpty(partialPath))
                partialPath = controller.ControllerContext.RouteData.GetRequiredString("action");
    
            controller.ViewData.Model = model;
    
            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialPath);
                ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                // copy model state items to the html helper 
                foreach (var item in viewContext.Controller.ViewData.ModelState)
                    if (!viewContext.ViewData.ModelState.Keys.Contains(item.Key))
                    {
                        viewContext.ViewData.ModelState.Add(item);
                    }
    
    
                viewResult.View.Render(viewContext, sw);
    
                return sw.GetStringBuilder().ToString();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多