码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC5--杂学

时间:2015-06-20 18:17:27      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

1.在默认的MVC中,是根据路由来显示的。例如:

技术分享

通过这个链接,

@Html.ActionLink("Edit", "Edit", new { id=item.ID }) 

*The Html object is a helper that‘s exposed using a property on the System.Web.Mvc.WebViewPage base class. TheActionLink method of the helper makes it easy to dynamically generate HTML hyperlinks that link to action methods on controllers. The first argument to the ActionLink method is the link text to render (for example,<a>Edit Me</a>). The second argument is the name of the action method to invoke (In this case, the Edit action). The final argument is an anonymous object that generates the route data (in this case, the ID of 4).

这段话的意思是:ACtionLink方法帮我们自动生成了HTML超链接,去定向到控制器的方法中。第一个参数是链接的文本,第二个参数是要链接的方法,第三个参数是一个匿名对象,可以生成路由数据(在这个例子中,Id为4).


路由代码:
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Routing;
 7 
 8 namespace MvcMovie
 9 {
10     public class RouteConfig
11     {
12         public static void RegisterRoutes(RouteCollection routes)
13         {
14             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
15 
16             routes.MapRoute(
17                 name: "Default",
18                 url: "{controller}/{action}/{id}",
19                 defaults: new { controller = "Movies", action = "Index", id = UrlParameter.Optional }
20             );
21         }
22     }
23 }
路由代码
2.我们注意到:这里的地址栏中是根据路由来的,我们也可以通过使用QueryString的方式来定制地址栏显示的地址。
You can also pass action method parameters using a query string. For example, the URLhttp://localhost:1234/Movies/Edit?ID=3 also passes the parameter ID of 3 to the Edit action method of the Moviescontroller.

*首先:我们打开控制器Movie,找到Edit方法。
技术分享
 1     // GET: Movies/Edit/5
 2         public ActionResult Edit(int? id)
 3         {
 4             if (id == null)
 5             {
 6                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
 7             }
 8             Movie movie = db.Movies.Find(id);
 9             if (movie == null)
10             {
11                 return HttpNotFound();
12             }
13             return View(movie);
14         }
15 
16         // POST: Movies/Edit/5
17         // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关 
18         // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598
19         [HttpPost]
20         [ValidateAntiForgeryToken]
21         public ActionResult Edit([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Movie movie)
22         {
23             if (ModelState.IsValid)
24             {
25                
26                 db.Entry(movie).State = EntityState.Modified;
27                 db.SaveChanges();
28                 return RedirectToAction("Index");
29             }
30             return View(movie);
31         }
Edit方法

*The Bind attribute is another important security mechanism that keeps hackers from over-posting data to your model. You should only include properties in the bind attribute that you want to change. 

这句话的意思是:Bind属性是一个很重要的安全机制,它可以防止黑客post数据到你的Model中,你应该只包含你要修改的字段到bind里面。

*The ValidateAntiForgeryTokenattribute is used to prevent forgery of a request and is paired up with @Html.AntiForgeryToken() in the edit view file (Views\Movies\Edit.cshtml), a portion is shown below:  (这句话的意思是,ValidateAntiForgeryToken属性,和编辑视图里面的@Html.AntiForgeryToken()方法,共同用来防止一个伪造的请求)

技术分享

 

*@Html.AntiForgeryToken() generates a hidden form anti-forgery token that must match in the Edit method of the Movies controller. You can read more about Cross-site request forgery (also known as XSRF or CSRF) in my tutorial XSRF/CSRF Prevention in MVC.(这句话的意思是:@Html.AntiForgeryToken()生成了一个隐藏域,而且必须要和控制器中的对应方法匹配)

*Run the application and navigate to the /Movies URL. Click an Edit link. In the browser, view the source for the page. The HTML for the form element is shown below.(运行项目,查看源文件,)

技术分享

 

3.我们在编辑的时候,不能编辑价格像类似这样用逗号分隔的”22,55“而是只能是22.55,这里有个解决方案:

技术分享
1 Note to support jQuery validation for non-English locales that use a comma (",") for a decimal point, and non US-English date formats, you must include globalize.js and your specific cultures/globalize.cultures.js file(from https://github.com/jquery/globalize ) and JavaScript to use Globalize.parseFloat. You can get the jQuery non-English validation from NuGet. (Don‘t install Globalize if you are using a English locale.)
Note
1 1.From the Tools menu click Library Package Manager, and then click Manage NuGet Packages for Solution.
2 
3 2.On the left pane, select Online. (See the image below.)
4 
5 3.In the Search Installed packages input box, enter Globalize.

技术分享

 Click Install. The Scripts\jquery.globalize\globalize.js file will be added to your project. The Scripts\jquery.globalize\cultures\ folder will contain many culture JavaScript files. Note, it can take five minutes to install this package.The following code shows the modifications to the Views\Movies\Edit.cshtml file:

 1 @section Scripts {
 2     @Scripts.Render("~/bundles/jqueryval")
 3 
 4 <script src="~/Scripts/globalize/globalize.js"></script>
 5 <script src="~/Scripts/globalize/cultures/globalize.culture.@(System.Threading.Thread.CurrentThread.CurrentCulture.Name).js"></script>
 6 <script>
 7     $.validator.methods.number = function (value, element) {
 8         return this.optional(element) ||
 9             !isNaN(Globalize.parseFloat(value));
10     }
11     $(document).ready(function () {
12         Globalize.culture(@(System.Threading.Thread.CurrentThread.CurrentCulture.Name));
13     });
14 </script>
15 <script>
16     jQuery.extend(jQuery.validator.methods, {
17         range: function (value, element, param) {
18             //Use the Globalization plugin to parse the value
19             var val = Globalize.parseFloat(value);
20             return this.optional(element) || (
21                 val >= param[0] && val <= param[1]);
22         }
23     });
24     $.validator.methods.date = function (value, element) {
25         return this.optional(element) ||
26             Globalize.parseDate(value) ||
27             Globalize.parseDate(value, "yyyy-MM-dd");
28     }
29 </script>
30 }

还要在web配置文件中,添加这一行代码;

1  <system.web>
2     <globalization culture ="en-US" />
3     <!--elements removed for clarity-->
4   </system.web>

 

ASP.NET MVC5--杂学

标签:

原文地址:http://www.cnblogs.com/caofangsheng/p/4590750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!