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

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

问题描述

我对 Web 应用程序开发非常陌生,我想我会从最近的技术开始,所以我正在尝试同时学习 asp.net 以及 MVC 框架.对于 MVC 专业人士来说,这可能是一个非常简单的问题.

我的问题是分部视图是否应该有关联的动作,如果是这样,当普通页面在分部视图上使用 RenderPartial() 时,是否会调用该动作?

解决方案

虽然您可以有一个返回分部视图的操作,但您不需要一个操作来呈现分部视图.RenderPartial 获取局部视图并使用给定的模型和视图数据(如果提供)将其渲染到当前(父)视图中.

如果您使用 AJAX 加载/重新加载页面的一部分,您可能需要一个返回部分视图的操作.在这种情况下,不需要返回完整视图,因为您只想重新加载页面的一部分.在这种情况下,您可以让操作只返回与页面该部分相对应的部分视图.

标准机制

在普通视图中使用局部视图(无需操作)

...一些html...<% Html.RenderPartial( "Partial", Model.PartialModel );%>...更多 html..

Ajax 机制

通过 AJAX 重新加载页面的一部分(注意部分在初始页面加载时内联呈现)

...一些html...<div id="部分"><% Html.RenderPartial( "Partial", Model.PartialModel );%>

...更多 html...<script type="text/javascript">$(函数(){$('#someButton').click( function() {$.ajax({url: '/控制器/动作',数据:......一些行动数据......,数据类型:'html',成功:功能(数据){$('#partial').html(data);},...});});});

AJAX 控制器

public ActionResult Action(...){无功模型= ......如果 (Request.IsAjaxRequest()){返回 PartialView( "Partial", model.PartialModel );}别的{返回视图(模型);}}

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.

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?

解决方案

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.

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.

Standard mechanism

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

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

Ajax mechanism

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>

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天全站免登陆