简体   繁体   English

ASP.NET MVC部分视图呈现

[英]ASP.NET MVC partial views rendering

I'm a beginner in ASP.NET MVC and I want to have a website like this : 我是ASP.NET MVC的初学者,我想拥有一个像这样的网站:

  • A main page that load all the css/js files, subscribe to JavaScript events, store JavaScript variables 加载所有css / js文件,订阅JavaScript事件,存储JavaScript变量的主页
  • Partial views that replace the page content without reloading it, and keep the JavaScript variables,etc... 部分视图可替换页面内容而不重新加载页面内容,并保留JavaScript变量等。

What is the best way to to it ? 最好的方法是什么?

I tried : 我试过了 :

        $.ajax({
            url: '@Url.Action("Index", "Home")',
            data: { cardid: currentCardId },
            cache: false,
            type: "POST",
            dataType: "html",
            success: function (data) {
                SetData(data);
            }
        });

function SetData(data) {
    alert(data);
    $("#divPartialView").html(data);
}

In my HomeController, I return a PartialView(); 在我的HomeController中,我返回一个PartialView();。

(It doesn't work yet but it should) (它尚不可用,但应该可以)

Is there a simple/better way ? 有没有简单/更好的方法?

I also tried this but it doesn't work : 我也试过了,但是没有用:

$('#divPartialView').load(@Html.Action("Index", "Home")));

Also, Can I define the Model in the PartialView ? 另外,我可以在PartialView中定义模型吗?

your example should work 你的例子应该工作

This is a working example 这是一个有效的例子

$.ajax({
      type: 'POST',
      url: '@Url.Action("ActionName", "ControllerName")',
      data: { id: $('#mycomponent').attr('data-id')},
      success: function (data2) {
                    $('#mydiv').html(data2);
               },
      error: function (a, b, c) {
            alert(a);
      }
});

my controller 我的控制器

public ActionResult ActionName(int id)
        {
            return PartialView("mypartialViewPath",id);
        }

partial view 部分视图

@model int
<div>
...content
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM