Integrating jQuery UI and ASP.NET MVC

本文展示了如何将jQueryUI整合到ASP.NET MVC应用中,以实现动态和响应式的用户界面。通过简单的步骤,我们展示了如何创建一个包含jQueryUI功能的ASP.NET MVC应用模板,并详细介绍了如何修改标准项目模板以支持jQueryUI。此外,文章还讨论了如何在应用中使用jQueryUI来替代静态标签,以及如何调整页面标题以适应动态内容的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

The jQuery UI library is an excellent tool for building dynamic and highly responsive user interfaces. Writing a simple view to showcase its capabilities is no big deal, and this is definitely a great statement about the library itself. For example, if you’re looking for a tabstrip component—a commonly requested feature—jQuery UI offers a client-side solution that allows you to switch between tabs without causing a full page refresh. If the content of the tab is not static and is available somewhere in the current DOM, it can be downloaded from a remote location, but the request is sent asynchronously using the classic XMLHttpRequest object. To arrange a tabstrip, you need only to create an ad hoc HTML structure and then call a jQuery UI starter function on the root element of the tree.

As I mentioned, showcasing jQuery UI is extremely easy, but using it in real-world views and complex pages is a bit more problematic. The biggest issue is not so much the functionality itself, which remains relatively easy to arrange and exploit. If you’re starting with a blank page, adding jQuery UI is not hard. However, if you have a master page or are modifying an existing master page to support jQuery UI—a very common scenario these days—then be prepared to face some issues. You won’t run into anything that really prevents you from using jQuery UI effectively and extensively, but you do face some design considerations and some slight restructuring of your server code.

In this article, I’ll show how to modify the standard project template of ASP.NET MVC 2 applications to incorporate jQuery UI functionalities. The final result is the skeleton of an ASP.NET MVC application whose front-end implements a jQuery UI tabstrip. The final code is only the starting point for a fully AJAX-enabled ASP.NET MVC application.

Setup of the Project

One of the conventions of ASP.NET MVC applications is storing all auxiliary resources, like CSS and images, under the Content folder and all script files under the Scripts folder. These conventions can be changed without affecting any aspects of the run-time behavior, but I don’t see any reasons to make the change. So let’s start with the default ASP.NET MVC project and assume that the Content folder contains a subdirectory with all the files for the selected jQuery UI theme. Also, let’s assume that all jQuery UI script files live under the Scripts folder. Figure 1 gives you an idea of the project folder.


Figure 1. The project folder after the first step.

In addition, the site.master file will link the script files for the library. Here’s an excerpt from the master file that shows the actual content of the <head> section:

  1. <head runat="server">
  2. <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
  3. <link href="../../Content/Site.css" rel="stylesheet" rel="stylesheet"
  4. type="text/css" />
  5. <link href="../../Content/jq-ui-Themes/sunny/jquery-ui-sunny.css" rel="stylesheet"
  6. type="text/css" />
  7. <script type="text/javascript"
  8. src="../../Scripts/jquery-1.4.1.min.js" />
  9. <script type="text/javascript"
  10. src="../../Scripts/jQueryUI/jquery-ui-1.7.2.custom.min.js" />
  11. </head>

This ends the preliminaries of the project setup. The jQuery UI library is now ready for use.

The Master Page

The default master page is shown in Figure 2. As you can see, it offers a couple of tabs that remain in every view created from the master page. The idea is to transform these static tabs into a more dynamic structure in which tabs are created and managed via the script code injected by jQuery UI.


Figure 2. The default master page of ASP.NET MVC applications.

The tabs are created using the following code:

  1. <div id="menucontainer">
  2. <ul id="menu">
  3. <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
  4. <li><%= Html.ActionLink("About", "About", "Home")%></li>
  5. </ul>
  6. </div>

Nicely enough, this HTML structure is semantically correct for jQuery UI to work and doesn’t really need significant changes. The following code is a slightly edited version that includes a couple more tabs plus some extra information about the title string to display:

  1. <div id="menucontainer">
  2. <ul id="menu">
  3. <li><%= Html.ActionLink("Home", "Index", "Home",
  4. new { title="Home Page" }, null)%></li>
  5. <li><%= Html.ActionLink("About", "About", "Home",
  6. new { title = "About" }, null)%></li>
  7. <li><%= Html.ActionLink("Misc", "Demo", "Home",
  8. new { title = "Miscellaneous demos" }, null)%></li>
  9. <li><%= Html.ActionLink("What's up here", "Todo", "Home",
  10. new { title = "Read me" }, null)%></li>
  11. </ul>
  12. </div>

Why is the title string specified as an argument?

When you have a client-side tabstrip activated via script code, the change of the title doesn’t happen automatically. In a classic non-AJAX scenario, every click on a tab causes a new page to be assembled, mixing together master and view. In this process, you set the page title on the server. You need some script code to update the page title when you switch to another tab, and the string for the title must be passed in some way. The code shown earlier adds the title attribute to the hyperlink, and this has a double benefit. On one hand, it attaches a tooltip for when the user hovers over the tab, and on the other, it stores the string in a DOM attribute where you can retrieve it programmatically.

Turning Action Links into Tabs

As I mentioned, the menucontainer DIV block you find in the standard ASP.NET MVC master page is just perfect for providing the scaffolding for a jQuery UI tabstrip. All you need to do is attach a bit of script code that runs when each page based on the master is loaded. You can use jQuery—not jQuery UI—facilities and register a handler for the document’s ready event. Alternatively, you can use any similar facilities that your AJAX library of choice offers. These days jQuery is pretty popular, and everyone seems to confirm that it will only grow in the future. So here’s the jQuery code you need:

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $("menucontainer").tabs();
  4. });
  5. </script>

Figure 3 shows the new look of the page once it is equipped with jQuery UI.


Figure 3—A jQuery UI tabstrip in lieu of static tabs.

There are a couple of things to notice and understand in more depth. The first regards the home page of the site, and the second is about the title of the page and how it changes when the user moves through the tabs.

The default.aspx Template

In a normal ASP.NET MVC application, you don’t need any default.aspx file. The only exception is when your ASP.NET MVC application is hosted under IIS 6 or under IIS 7 but configured to run in Classic mode (as opposed to Pipeline mode). Under normal circumstances, the home page of an ASP.NET MVC application results from the composition of the master page with the Index.aspx template. In addition, the Index method on the Home controller is the default action that determines what the end user sees on her first visit to the site. So what’s this default.aspx template all about?

When you use jQuery UI to power up your ASP.NET MVC site, you need an extra container page where you stuff the DIV block and any related script code for the tabstrip. The Index method on the Home controller—or whatever action you decide to call to populate your home page—is no longer responsible for the entire home page but only for the content that goes into the tab. More concretely, this means that you need an additional template and controller action to contain the tabstrip. I decided to call the method Default, and given the code below, the host view is default.aspx. It goes without saying that you can change this name at your leisure. The implementation of the method Default, as well as the structure of the default.aspx view, isn’t really rocket science. Here’s how it looks:

  1. public class HomeController : Controller
  2. {
  3. public ActionResult Default()
  4. {
  5. return View();
  6. }
  7. :
  8. }

Similarly, the markup in the default.aspx view is fairly trivial stuff:

  1. <%@ Page Language="C#"
  2. MasterPageFile="~/Views/Shared/Site.Master"
  3. Inherits="System.Web.Mvc.ViewPage" %>
  4. <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
  5. Nice UI
  6. </asp:Content>

Finally, some changes are required also in the global.asax file; specifically, in the section where the supported routes are defined and registered:

  1. public class MvcApplication : System.Web.HttpApplication
  2. {
  3. public static void RegisterRoutes(RouteCollection routes)
  4. {
  5. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  6. routes.MapRoute(
  7. "Default",
  8. "{controller}/{action}/{id}",
  9. new {
  10. controller = "Home",
  11. action = "Default",
  12. id = UrlParameter.Optional
  13. }
  14. );
  15. }
  16. protected void Application_Start()
  17. {
  18. AreaRegistration.RegisterAllAreas();
  19. RegisterRoutes(RouteTable.Routes);
  20. }
  21. }

As you can see, the default action is now Default in lieu of the standard Index method. This means that the role and content of the views linked from the tabstrip are different from a standard ASP.NET MVC application.

Let’s have a look at the code of the Index method and related view:

  1. public ActionResult Index()
  2. {
  3. ViewData["Message"] = "Welcome to the <span id=\"hiTitle\">
  4. MVC+jQueryUI</span> template!";
  5. return View();
  6. }

As you can see, there’s nothing special in the Index method and in any of the other methods linked from the tabstrip. A key difference instead lives in the Index.aspx view:

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
  2. <script type="text/javascript">
  3. $(document).ready(function() {
  4. :
  5. });
  6. </script>
  7. <div>
  8. <table>
  9. <tr>
  10. <td>
  11. <h2><%= ViewData["Message"] %></h2>
  12. <p>
  13. To learn more about ASP.NET MVC visit
  14. <a href="http://asp.net/mvc"
  15. title="ASP.NET MVC Website"> http://asp.net/mvc </a>.
  16. </p>
  17. <p>
  18. To learn more about jQuery UI visit
  19. <a href="http://jqueryui.com"
  20. title="jQuery UI Website"> http://jqueryui.com </a>.
  21. </p>
  22. </td>
  23. <td valign="top">
  24. :
  25. </td>
  26. </tr>
  27. </table>
  28. </div>

The raw content is nearly the same as in the analogous view from the standard project template. A huge difference is that now, with jQuery UI in place and a new default.aspx view around, Index and any other view linked from the tabstrip are partial views. As a result, they don’t build the entire page and don’t need to inherit from the master page. The Index view simply returns a chunk of HTML that is merged within the template of the default.aspx view.

The Home tab you see in Figure 3 is associated with the /Home/Index URL, and a similar ASP.NET MVC URL is attached to any tabs. When the user clicks, the tabstrip jQuery UI component connects to the URL via AJAX and downloads the response. Next, the response is inserted—as is—in the current DOM in the subtree that represents the content of the clicked tab. For this precise reason, the URL can’t return a full page view but must be limited to returning a partial view. In terms of ASP.NET MVC programming, this simply means that you have to adjust the view so that it returns a chunk of HTML. You obtain this by simply removing dependencies on the master page. Alternatively, you can achieve the same result by writing a user control and returning it via PartialView from the method, as shown here:

  1. public ActionResult Index()
  2. {
  3. :
  4. return PartialView(); // assumed index.ascx under Views
  5. }

In summary, when you switch to a jQuery UI template you must assume a host that is a classic ASP.NET MVC page and one partial view for each tab. The markup for the tab is automatically downloaded via AJAX, and the content is cached as any other Web resource.

Note that in this jQuery UI template, if you request /Home/Index from the address bar, you get a script error or a scanty, black-and-white HTML page, depending on the user’s settings for scripting within the browser.

Changing the Page Title

In classic ASP.NET MVC, when tabs are just plain links to navigate to another page, each view can easily set the page title. When the user tabs to Home/About, the About view quite simply sets the page title using any tools available—a piece of script code, a placeholder from the master page, or even the API offered by the code-behind ViewPage class.

How would you set the page title instead when the Home/Index (and similar) URLs are just endpoints for partial views? In this case, you rely on the tabstrip API and intercept the user’s clicking that changes the selection to a new tab. Because this script code is common to all jQuery UI based pages, you can insert it into the master page or into the default.aspx host page. Here’s the code you need:

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $("menucontainer").tabs();
  4. $("menucontainer").bind( 'tabsselect',
  5. function( event, ui ) {
  6. document.title = ui.tab.title;
  7. }
  8. );
  9. });
  10. </script>

As you saw earlier, the first call just sets up the tabstrip. The second call registers an event handler for the tabsSelect event fired by the jQuery UI tabstrip. The event is fired whenever the user changes the selection. The code to run in this case is specific through the anonymous function parameter. You can see that all it does is set the title of the current document—the page—to the title property of the currently selected tab. The expression ui.tab is used to refer to the root of the DOM subtree that represents the tab. The value assigned to the title attribute is the same value you passed in as the title attribute when you created the link for the tab. In other words, the same string is used for both the tooltip of the tab and for the page title when that tab is selected.

Summary

With the jQuery library gaining momentum and being fully embraced by Microsoft, the jQuery UI library is also destined to be used more and more. ASP.NET MVC represents the new frontier of ASP.NET development, and it is strongly characterized by the control it provides over markup. Control over markup means control over every bit of HTML—but also over the script code being emitted to the browser. For this reason, the combination of ASP.NET MVC and jQuery has already been welcomed as a powerful duo. The jQuery UI library just adds more spice to an already sapid dish.

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在机器人技术中,轨迹规划是实现机器人从一个位置平稳高效移动到另一个位置的核心环节。本资源提供了一套基于 MATLAB 的机器人轨迹规划程序,涵盖了关节空间和笛卡尔空间两种规划方式。MATLAB 是一种强大的数值计算与可视化工具,凭借其灵活易用的特点,常被用于机器人控制算法的开发与仿真。 关节空间轨迹规划主要关注机器人各关节角度的变化,生成从初始配置到目标配置的连续路径。其关键知识点包括: 关节变量:指机器人各关节的旋转角度或伸缩长度。 运动学逆解:通过数学方法从末端执行器的目标位置反推关节变量。 路径平滑:确保关节变量轨迹连续且无抖动,常用方法有 S 型曲线拟合、多项式插值等。 速度和加速度限制:考虑关节的实际物理限制,确保轨迹在允许的动态范围内。 碰撞避免:在规划过程中避免关节与其他物体发生碰撞。 笛卡尔空间轨迹规划直接处理机器人末端执行器在工作空间中的位置和姿态变化,涉及以下内容: 工作空间:机器人可到达的所有三维空间点的集合。 路径规划:在工作空间中找到一条从起点到终点的无碰撞路径。 障碍物表示:采用二维或三维网格、Voronoi 图、Octree 等数据结构表示工作空间中的障碍物。 轨迹生成:通过样条曲线、直线插值等方法生成平滑路径。 实时更新:在规划过程中实时检测并避开新出现的障碍物。 在 MATLAB 中实现上述规划方法,可以借助其内置函数和工具箱: 优化工具箱:用于解决运动学逆解和路径规划中的优化问题。 Simulink:可视化建模环境,适合构建和仿真复杂的控制系统。 ODE 求解器:如 ode45,用于求解机器人动力学方程和轨迹执行过程中的运动学问题。 在实际应用中,通常会结合关节空间和笛卡尔空间的规划方法。先在关节空间生成平滑轨迹,再通过运动学正解将关节轨迹转换为笛卡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值