Three steps to use jQuery UI in ASP.NET MVC 5

Many developers struggle to work with jQuery UI in an ASP.NET MVC application. In this post, I will show you three steps required to start working withjQuery UI in an ASP.NET MVC application. At the end of the post we will take a look at working with the autocomplete widget.
The following three steps will let you work with jQuery UI in an ASP.NET MVC application:
Step 1: Add the jQuery UI Reference
Add the jQuery UI reference into the project using the NuGet manager. Once this is done, you should find the reference added in the Content folder and the Scripts folder.

Step 2: Bundle the required files
Open the BundleConfig.cs file. In this file add two entries, one for the jQuery UI scripts and other for jQuery UI CSS.
Add the script entry as follows:

1

2

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(

"~/Scripts/jquery-ui-{version}.js"));

Next add the CSS files for jQueryUI widgets.  CSS for all the widgets can be bundled like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(

"~/Content/themes/base/jquery.ui.core.css",

"~/Content/themes/base/jquery.ui.resizable.css",

"~/Content/themes/base/jquery.ui.selectable.css",

"~/Content/themes/base/jquery.ui.accordion.css",

"~/Content/themes/base/jquery.ui.autocomplete.css",

"~/Content/themes/base/jquery.ui.button.css",

"~/Content/themes/base/jquery.ui.dialog.css",

"~/Content/themes/base/jquery.ui.slider.css",

"~/Content/themes/base/jquery.ui.tabs.css",

"~/Content/themes/base/jquery.ui.datepicker.css",

"~/Content/themes/base/jquery.ui.progressbar.css",

"~/Content/themes/base/jquery.ui.theme.css"));

For the purpose of this example, let’s say you are only working with the autocomplete widget. In this case, you would only bundle the core.css andautocomplete.css as shown below:

1

2

3

4

bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(

"~/Content/themes/base/jquery.ui.core.css",

"~/Content/themes/base/jquery.ui.autocomplete.css",

"~/Content/themes/base/jquery.ui.theme.css"));

Step 3: Refer to the Bundles
Once the bundles for jQuery UI have been created, you need to add them to the layout file. That can be done as follows:

1

2

3

4

5

6

@Styles.Render("~/Content/css")

@Styles.Render("~/Content/themes/base/css")

@Scripts.Render("~/bundles/modernizr")

@Scripts.Render("~/bundles/jquery")

@Scripts.Render("~/bundles/bootstrap")

@Scripts.Render("~/bundles/jqueryui")

By default you will find the jQuery bundle at the end of the layout file. To work with jQuery UI widgets, you should move the jQuery bundle to the top of the file and also include the bundles for jQuery UI CSS and Scripts.
You have now completed the three steps necessary to work with jQueryUI in an ASP.NET MVC application.
Use jQueryUI Widgets 
Now let’s look at the autocomplete widget in action.  I have created a controller for returning JSON data as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

public ActionResult Index()

{

return View();

}

public ActionResult GetMP(string term)

{

var result = from r in _db.GetMPDetails()

where r.Name.ToLower().Contains(term)

select r.Name;

return Json(result, JsonRequestBehavior.AllowGet);

}

We will now bind the returned JSON from the GetMP() action to the autocomplete widget. On the razor view, you can create an autocomplete widget like this:

1

2

3

4

5

6

7

8

9

10

11

<input type="text" id="mpvalue" name="mpvalue" />

<script type="text/javascript">

$(document).ready(function () {

$(‘#mpvalue‘).autocomplete({

source: ‘@Url.Action("GetMP")‘

});

})

</script>

Make sure the view is using the layout file in which you added the reference of the bundles.
Summary 
We’ve seen in this post that working with jQuery UI widgets is as simple as following three simple steps:

  1. Add the  jQuery UI reference
  2. Bundle the required files
  3. Refer to the bundles

The autocomplete widget is also a helpful tool when working with jQuery UI bundles.
I hope this post was useful. Happy coding.

时间: 2024-10-22 01:53:26

Three steps to use jQuery UI in ASP.NET MVC 5的相关文章

jQuery Grid With ASP.Net MVC

jQuery Grid 能够在 ASP.NET MVC 中轻松地实现分页. 排序. 筛选以及 jQuery 插件网格中的 CRUD 操作. 具有以下特征: 时尚的表格数据呈现控件. JavaScript 控件用于表示和处理 web 上的表格数据. 可启用 Ajax. 可以与任何 ASP. JavaServelets. JSP. PHP 等服务器端的技术集成. 与 ASP.NET 集成,很简单. 支持分页.JavaScript 和服务器端数据源. 支持 jQuery UI 和引导(Bootstra

jQuery DataTables and ASP.NET MVC Integration

part 1 : http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part Introduction The jQuery DataTables plug-in is an excellent client-side component that can be used to create rich-functional tables in the web brows

kendo UI for asp.net mvc

Visual Studio 3013 插件下载 http://visualstudiogallery.msdn.microsoft.com/65b78c2c-951e-43a8-bae7-f9039f59fb9b 视频学习

ASP.NET MVC的客户端验证:jQuery的验证

http://www.cnblogs.com/artech/archive/2012/06/17/client-validation-01.html 之前我们一直讨论的Model验证仅限于服务端验证,即在Web服务器根据相应的规则对请求数据实施验证.如果我们能够在客户端(浏览器)对用户输入的数据先进行验证,这样会减少针对服务器请求的频率,从而缓解Web服务器访问的压力.ASP.MVC 2.0及其之前的版本采用ASP.NET Ajax进行客户端验证,在ASP.NET MVC 3.0中,jQuery

ASP.NET MVC的客户端验证:jQuery验证在Model验证中的实现

原文:ASP.NET MVC的客户端验证:jQuery验证在Model验证中的实现 在简单了解了Unobtrusive JavaScript形式的验证在jQuery中的编程方式之后,我们来介绍ASP.NET MVC是如何利用它实现客户端验证的.服务端验证最终实现在相应的ModelValidator中,而最终的验证规则定义在相应的ValidationAttribute中:而客户端验证规则通过HtmlHelper<TModel>相应的扩展方法(比如TextBoxFor.EditorFor和Edid

ASP.NET MVC 基础

ASP.NET MVC oo1 Mvc准备工作课程安排:ORM->AspNet MVC开发环境:VS2012/VS2013SqlServer2008/2005主讲Asp.Net Mvc4 Razor官网:http://www.asp.net/mvc/mvc4 002 自动属性知识储备:自动属性隐式类型 var匿名类对象初始化与集合初始化扩展方法Lambda表达式Product(Id,Name,Description,Price,Category) 003 var关键字var num=10;var

FineUIMvc v1.4.0 发布了(ASP.NET MVC控件库)!

FineUIMvc v1.4.0 已经于 2017-06-30 发布,FineUIMvc 是基于 jQuery 的专业 ASP.NET MVC 控件库,是我们的新产品.由于和 FineUI(专业版)共享前端库,所以从一开始功能就非常丰富,而且产品稳定可靠. 目前官网示例和版本记录已更新:官网示例:http://fineui.com/demo_mvc/更新记录:http://fineui.com/version_mvc/ FineUIMvc(基础版)完全免费 FineUIMvc(基础版)作为三石奉

asp.net+mvc+easyui+sqlite 简单用户系统学习之旅

首次接触asp.net开发,希望把自己的学习之旅写下来,一方面做个知识归纳技术总结,另一方面开放到博客中,和大家一起交流学习! asp.net是目前流行的web开发技术之一,是微软旗下开发的基于.net framework的一套免费的网络应用框架.因为其强大专业的后台支持,和配套优秀的vs开发工具,越来越成为web开发者和公司的选择.学习asp.net可以参考极客学院的知识体系图. http://www.jikexueyuan.com/path/aspdotnet/ 1. C#语言基础 2. A

[转]ASP.NET MVC 入门11、使用AJAX

在ASP.NET MVC beta发布之前,M$就宣布支持开源的JS框架jQuery,然后ASP.NET MVC beta发布后,你建立一个ASP.NET MVC beta的项目后,你可以在项目的scripts目录下找到ASP.NET AJAX和jQuery的JS.反正我是比较喜欢jQuery的,所以对于M$此举还是挺欣慰的. 废话不多说,我们使用AJAX来实现发表评论的功能吧.先来看看怎样使用M$的JS框架来进行异步AJAX请求. 首先,当然是要引入M$的AJAX框架的JS: <script