asp.net MVC局部视图控制器操作 [英] asp.net MVC partial view controller action

查看:129
本文介绍了asp.net MVC局部视图控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的Web应用程序的开发,我想我会开始与最新的技术,所以我正在努力学习asp.net的,以及MVC框架的一次。这可能是你一个很简单的问题,MVC的专业人才。

I'm very new to web app development and I thought I would start with recent technology and so I'm trying to learn asp.net as-well as the MVC framework at once. This is probably a very simple question for you, MVC professionals.

我的问题是应该的局部视图有相关的动作,如果是这样,那么这个动作被调用每当一个正常的页面使用的RenderPartial()的局部视图?

My question is should a partial view have an associated action, and if so, does this action get invoked whenever a normal page uses RenderPartial() on the partial view?

推荐答案

虽然你可以有一个返回局部视图的操作,你不需要一个动作渲染的局部视图。需要的RenderPartial局部视图和渲染它,使用给定的模型和视图的数据如果提供,到当前的(父)视图。

While you can have an action that returns a partial view, you don't need an action to render a partial view. RenderPartial takes the partial view and renders it, using the given model and view data if supplied, into the current (parent) view.

您可能想,如果你正在使用AJAX来加载/重新加载页面的一部分,它返回一个局部视图的操作。在这种情况下,返回众目睽睽不希望因为你只需要重新加载页面的一部分。在这种情况下,你可以有动作只是返回对应于页面的该部分的局部视图。

You might want an action that returns a partial view if you are using AJAX to load/reload part of a page. In that case, returning the full view is not desired since you only want to reload part of the page. In this case you can have the action just return the partial view that corresponds to that section of the page.

标准机制

制作一个普通视图中使用局部视图(无动作需要)

Making use of partial view within a normal view (no action needed)

...some html...
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
...more html..

阿贾克斯机制

通过AJAX重新加载页面的一部分(注意部分在初始页面加载内嵌渲染)

Reloading part of a page via AJAX (note partial is rendered inline in initial page load)

...some html...
<div id="partial">
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
</div>
...more html...

<script type="text/javascript">
   $(function() {
       $('#someButton').click( function() {
           $.ajax({
              url: '/controller/action',
              data: ...some data for action...,
              dataType: 'html',
              success: function(data) {
                 $('#partial').html(data);
              },
              ...
           });
       });
   });
</script>

控制器AJAX

Controller for AJAX

public ActionResult Action(...)
{
     var model = ...

     ...

     if (Request.IsAjaxRequest())
     {
          return PartialView( "Partial", model.PartialModel );
     }
     else
     {
          return View( model );
     }
}

这篇关于asp.net MVC局部视图控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆