您知道在 MVC 中通过 AJAX 更新复杂视图生成的 HTML 块的最佳方法是什么吗?

发布于 2024-08-17 00:06:11 字数 1375 浏览 3 评论 0原文

我的旧 Web 表单应用程序中有一堆代码,我正在迁移到 MVC。各种页面的某些部分需要由视图生成 HTML,然后由 AJAX 调用重新生成。很多 HTML 都非常复杂,因此它确实有助于将所有 html 生成保持在一个位置(最好是模板)。

我的问题是,有更好的方法吗?我应该使用部分视图还是其他什么?这将如何转化为 WebService 调用?我可以做一些更有效的事情吗?

基本上这就是我现在正在做的事情(这是一个非常基本的、蹩脚的例子,所以我道歉):

伪代码中:

标记

<!--- some html here -->
<div id="myContent">
  <%=StaticMethods.GetContent()%>
</div>
<button id="btnUpdate">Update</button>
<!--- some html here -->

AJAX Javascripty 的东西

$(document).ready(function() {
   $('#btnUpdate').click(function() {
      $.ajax({
         url:'MyService.asmx/MyWebServiceMethod',
         /*crap here*/
         success: function(result, status, code) {
            $('#myContent').html(result);
         }
      });
   });
});

静态方法

public static class StaticMethods {
   public static string GetContent() {
      var sb = new StringBuilder();
      var somethings = GetSomeRandomThings(); 
      //just an example. it's normally much more complex.
      foreach(var something in somethings) {
          sb.AppendFormat("<li>{0}</li>",something.Name);
      }
   }
}

MyWebService

public string MyWebServiceMethod() {
    return StaticMethods.GetContent();
}

I have a bunch of code in my old Web Forms app I'm migrating to MVC. There are sections of various pages that need to have HTML generated by the view and then regenerated by an AJAX call. A lot of this HTML is pretty complex, so it really helps to keep all of the html generation in one spot (preferrably a template).

My question is, is there a better approach to this? Should I be using partial views or something? How would that translate into the WebService call? Is there something more efficient I could be doing?

Basically this is what I'm doing now (this is a really basic, crappy, example so I apologize):

In Pseudo-code:

The Markup

<!--- some html here -->
<div id="myContent">
  <%=StaticMethods.GetContent()%>
</div>
<button id="btnUpdate">Update</button>
<!--- some html here -->

The AJAX Javascripty stuff

$(document).ready(function() {
   $('#btnUpdate').click(function() {
      $.ajax({
         url:'MyService.asmx/MyWebServiceMethod',
         /*crap here*/
         success: function(result, status, code) {
            $('#myContent').html(result);
         }
      });
   });
});

The Static Method

public static class StaticMethods {
   public static string GetContent() {
      var sb = new StringBuilder();
      var somethings = GetSomeRandomThings(); 
      //just an example. it's normally much more complex.
      foreach(var something in somethings) {
          sb.AppendFormat("<li>{0}</li>",something.Name);
      }
   }
}

MyWebService

public string MyWebServiceMethod() {
    return StaticMethods.GetContent();
}

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

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

发布评论

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

评论(2

也只是曾经 2024-08-24 00:06:11

您可以使用部分视图。但请记住,您必须将数据传递到分部视图。如果您的局部视图在任何地方都使用,那么它可能会很复杂。

如果您想要更独立的东西,您可以使用 MVC Future 的“RenderAction()”来使部分视图的行为类似于对常规操作的调用。因此,“部分视图”不依赖于当前呈现的操作。

请参阅此处的区别: http://blogs .intesoft.net/post/2009/02/renderaction-versus-renderpartial-aspnet-mvc.aspx

就个人而言,我会使用 RenderAction() 进行页面的初始渲染。更新面板的 AJAX 调用将指向 RenderAction 调用的相同操作。

麦克风

You can use partial views. But remember that you must pass the data to the partial view. If your partial view is used everywhere it could be complex.

If you want something more independent, you can use the MVC Future's "RenderAction()" to make a partial view act like a call to a regular action. So the "partial view" does not have a dependency to the current action being rendered.

See the difference here: http://blogs.intesoft.net/post/2009/02/renderaction-versus-renderpartial-aspnet-mvc.aspx

Personally, I would use RenderAction() for the initial rendering of the page. The AJAX calls to update the panel would point to the same action of the RenderAction call.

Mike

半岛未凉 2024-08-24 00:06:11

对于需要通过 AJAX 更新的内容,我绝对会使用部分视图。它会让你的生活变得更加轻松。

您只需调用从分部视图返回 HTML 的 url,而不是调用 WebService。

总之,您所要做的就是:
1. 在控制器中有一个返回分部视图的方法。
2. 为 AJAX 调用调用该 URL。
3. 完成!

I'd absolutely use partial views for content that needs to be updated via AJAX. It'll make your life a ton easier.

Instead of calling the WebService, you'd just call the url that returns the HTML from the partial view.

In summary, all you'd have to do is:
1. Have a method in your controller that returns a partial view.
2. Call that URL for your AJAX call.
3. Done!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文