码迷,mamicode.com
首页 > Web开发 > 详细

在asp.net mvc中使用PartialView返回部分HTML段

时间:2015-02-12 19:59:03      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

问题链接: MVC怎样实现异步调用输出HTML页面

 

该问题是个常见的 case, 故写篇文章用于提示新人。

 

在asp.net mvc中返回View时使用的是ViewResult,它继承自ViewResultBase 同一时候它还有个兄弟PartialViewResult

 

相信聪明的你已经知道了它俩的差别了,没错 一个用于返回总体,还有一个返回局部(部分)。

 

如果我有这样一个需求,输入username,然后返回相关信息。之前的做法可能会是用json格式来返回用户的相关信息,然后到页面去渲染相关

 

的HTML,假设产生的相关HTML比較大的话,我还是建议你沿用之前的方案(返回json),由于传输的数据少,响应快一些。

 

反之,PartialViewResult 则是返回部分HTML 的不错选择。

 

以下就让我们看下怎样使用PartialViewResult:

 

Layout.cshtml

 

<!DOCTYPE html>

<html>

<head>

    <title>@ViewBag.Title</title>

    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>

</head>

<body>

    @RenderBody()

</body>

</html>

 

Index.cshtml

 

@{

    ViewBag.Title = "Index";

    Layout = "~/Views/Shared/_Layout.cshtml";

}

<h2>

    PartialView Demo</h2>

<div>

    Please write your name here

    <input type=‘text‘ id=‘txtName‘ />

    <input type=‘button‘ value=‘submit‘ id=‘btnOK‘ />

</div>

<br />

<div id=‘content‘>

</div>

<script type="text/javascript">

    $(function () {

        $(‘#btnOK‘).click(function () {

            var data = { Name: $(‘#txtName‘).val()};                

            $.ajax({

                type: "POST",

                url: @Url.Action("PartialViewDemo", "Home"),

                data: data,

                datatype: "html",

                success: function (data) {

                        $(‘#content‘).html(data);                   

                },

                error: function () {

                    alert("处理失败!");

                }

            });

        });      

    });

</script>

 

ViewUserControl.cshtml (Partial View)

 

@model Sample.Models.PartialViewDemoViewModel 

<div> 

 

 

<h2>ViewUserControl.cshtml</h2> 

 

@Model.dt

<br /><br />

Hello~  @Model.Name 

</div>

 

or ViewUC.ascx   (View User Control)

 

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Vancl.Sample.Models.PartialViewDemoViewModel>" %>

 

<div>

 

<h2>ViewUC.ascx</h2> 

 

<%=Model.dt %>

 

<br /><br />

 

Hello~  <%=Model.Name %>

 

</div>

 

Model

 

public class PartialViewDemoViewModel

    {

        public string Name { set; get; }

        public DateTime? dt { set; get; }

    }

 

Action

 

[HttpPost]

        public ActionResult PartialViewDemo(PartialViewDemoViewModel model)

        {

            model.dt = DateTime.Now;

            return PartialView("ViewUserControl", model); 

            //return PartialView("ViewUC", model); 

        } 

 

调用 Controller.PartialView方法时,能够指定 Partial View or View User Control 效果是一样的

 

不写后缀时,会查找同文件夹和Shared文件夹下的文件,也就是在同文件夹或Shared文件夹下时能够省略后缀名。

 

假设文件夹下存在同名的情况,会找第一个并返回。

 

eg: 同文件夹下有 ViewUserControl.ascx ViewUserControl.cshtml

 

这时使用 return PartialView("ViewUserControl");

 

会返回 ViewUserControl.ascx 的内容,由于字母ac :)

 

假设在这样的情况下想调用 ViewUserControl.cshtml

 

则须要写全路径,return PartialView("~/Views/Home/ViewUserControl.cshtml");

 

当想訪问的 Partial View or View User Control 在不同文件夹时,也能够通过全路径的方式訪问。

 

Hope this helps,

Sandy

 

在asp.net mvc中使用PartialView返回部分HTML段

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/4288815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!