asp.net MVC 部分视图控制器操作

发布于 2024-08-04 03:57:08 字数 172 浏览 7 评论 0原文

我对 Web 应用程序开发非常陌生,我想我应该从最新的技术开始,所以我尝试立即学习 ASP.NET 以及 MVC 框架。对于 MVC 专业人士来说,这可能是一个非常简单的问题。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

灯角 2024-08-11 03:57:08

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

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

标准机制

在普通视图中使用部分视图(无需任何操作)

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

Ajax 机制

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

...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 控制器

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

     ...

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

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 );
     }
}
坠似风落 2024-08-11 03:57:08

接受的答案是完全正确的,但我想补充一点,您可以使用 jQuery load 加载部分视图。如果您不想考虑并发性,则需要较少的配置。

$("#Your-Container").load("/controller/action/id");

The accepted answer is completely correct, but I want to add that you can load your partial view using jQuery load. Less configuration needed, if you don't want to consider concurrency.

$("#Your-Container").load("/controller/action/id");
守护在此方 2024-08-11 03:57:08

我能够用这种逻辑实现类似的目标。

在 .cshtml 内

@Html.Action("ActionMethodName", "ControllerName");

在控制器内

[Route("some-action")]
public ActionResult ActionMethodName()
{
    var someModel = new SomeModel();
    ...
    return PartialView("SomeView.cshtml", someModel);
}

就是这样。

如果您需要将值从 .cshtml 传递到操作方法,那么这是可以的。

I was able to achieve something similar with this logic.

Within the .cshtml

@Html.Action("ActionMethodName", "ControllerName");

Within the controller

[Route("some-action")]
public ActionResult ActionMethodName()
{
    var someModel = new SomeModel();
    ...
    return PartialView("SomeView.cshtml", someModel);
}

And that's it.

If you need to pass values from the .cshtml to the action method then that is possible to.

靖瑶 2024-08-11 03:57:08

答案是否定的。但有时您需要在部分视图后面进行一些控制器操作。然后您可以创建一个返回部分视图的actionMethod。这个actionMethod可以在另一个视图中调用:

@Html.Action("StockWarningsPartial", "Stores")

actionmethod可以看起来像:

public ActionResult StockWarningsPartial()
{
      ....              
      return View("StockWarningsPartial", warnings);

}

并且视图“StockWarningsPartial.cshtml”以:开头,

@{
    Layout = null;
}

以使其不会再次渲染周围的布局。

The answer is no. But sometimes you need some controller action behind a partial view. Then you can create an actionMethod wich returns a partial view. This actionMethod can be called within another view:

@Html.Action("StockWarningsPartial", "Stores")

The actionmethod can look like:

public ActionResult StockWarningsPartial()
{
      ....              
      return View("StockWarningsPartial", warnings);

}

and the view 'StockWarningsPartial.cshtml' starts with:

@{
    Layout = null;
}

to make it not render your surrounding layout again.

〃安静 2024-08-11 03:57:08
 public ActionResult GetStateList(int country_id)
 {
      List<stateDTO> stateList = new List<stateDTO>();
      stateList = bll.GetState(country_id);
      ViewBag.sList = new SelectList(stateList, "state_id", "State_Name");
      return PartialView("DisplayStates");
 }
 public ActionResult GetStateList(int country_id)
 {
      List<stateDTO> stateList = new List<stateDTO>();
      stateList = bll.GetState(country_id);
      ViewBag.sList = new SelectList(stateList, "state_id", "State_Name");
      return PartialView("DisplayStates");
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文