ASP.NET 項目總結

來源:互聯網
上載者:User

標籤:.net   csv   mvc   entity framework   

恩,第一次用 ASP.NET MVC 開發正式項目。好累。一直在摳細節, 現在把這一個礼拜學到的東西總結下, 新手要多總結!

一、JS 總結

首先, 載入 JS 檔案。在 .cshtml 檔案中, 載入 js 有很多方式, 可以直接用標籤載入進來。

@section HeaderJS{    <link rel="stylesheet" type="text/css" href="../Content/css/indexstyle.css" />    <link rel="stylesheet" href="../Content/js/ZTree/css/zTreeStyle/zTreeStyle.css">    <script src="../Content/js/ZTree/js/jquery.ztree.all-3.5.js"></script>    <script src="../Scripts/tableExport.js"></script>    <script src="../Scripts/jquery.base64.js"></script>    <script src="../../Content/js/public.js"></script>    <script type="text/javascript">}

從以上例子可以看出,這個方法可以用來載入 .js, .css 檔案到 .cshtml 檔案中。 你可以定義自己的 .js 檔案, 然後用這種方式載入進來。這樣你就可以在這個檔案裡面使用 .js 檔案中的函數了。 如 public.js 檔案如下:

//格式化Json資料中的時間function formatTime(dateTime) {    var date = new Date(parseInt(dateTime.replace("/Date(", "").replace(")/", ""), 10));    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();    var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();    var min = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();    return date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + min;}


這樣就可以直接在 HeaderJS 這個 section 中使用 formateTime() 函數。

zTree 的使用。

在另外文章中講解。

ASP.NET MVC 3 匯出匯入 HTML table 到 csv 檔案

在另外文章中講解。

HTML checkbox 全選和全不選的實現

function changeCheckBoxState() {        $("#chkcheckall").click(function () {            $('.radio').prop("checked", this.checked);        });     }

這裡有個問題是不能使用 checkbox 的 change 事件。 在 IE 中, 第一次點中 checkbox 並不觸發 change 事件, 直接用 click 事件繞開這個問題。

判斷瀏覽器是否是 IE

function isIE() { //ie?                if (!!window.ActiveXObject || "ActiveXObject" in window) {                    return true;                }                if (navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie") == "-1") {                    return false;                } else {                    return true;                }            }

這個又敗給 IE 了, IE 11 的 userAgent 裡面根本沒有 "ie" 字元, 這得坑掉多少人。第一個 if 語句是用來判斷是否是 IE 11 的。

二、ASP.NET MVC 總結

首先, Controller

在 Controller 中,向 view 返回的方式有很多, 比如:

return Content("ok");

//        // 摘要:        //     使用字串建立一個內容結果對象。        //        // 參數:        //   content:        //     要寫入到響應的內容。        //        // 返回結果:        //     內容結果執行個體。return Content("ok");//        // 摘要:        //     建立 System.Web.Mvc.JsonResult 對象,該對象使用指定 JSON 請求行為將指定對象序列化為 JavaScript 物件標記法        //     (JSON) 格式。        //        // 參數:        //   data:        //     要序列化的 JavaScript 對象圖。        //        //   behavior:        //     JSON 請求行為。        //        // 返回結果:        //     將指定對象序列化為 JSON 格式的結果對象。return Json(data, JsonRequestBehavior.AllowGet);//        // 摘要:        //     建立一個將視圖呈現給響應的 System.Web.Mvc.ViewResult 對象。        //        // 返回結果:        //     將視圖呈現給響應的視圖結果。return View();//        // 摘要:        //     建立一個呈現分部視圖的 System.Web.Mvc.PartialViewResult 對象。        //        // 返回結果:        //     分部視圖結果對象。return PartialView();

這個需要去看 MSDN 文檔, 隨時都會用到這些方法, 需要知道他們到底是幹嘛的。 在 System.Web.Mvc.Controller 中有介紹。

三、Entity Framework 總結
         

Entity Framework 是一款很好用的 OA 架構。 非常容易上手, 如果項目小, 不想用資料庫, 可以利用 EF 來產生本地的 .mdb 檔案, 作為資料存放區工具。 遇到的一個問題是,在串連遠端資料庫是,  伺服器總管能夠連到資料庫, 但是 EF 老是報無法開啟串連的錯誤。 這肯定是 url 串連資訊設定錯誤。

  <connectionStrings>    <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Mvcpro1-20141022154431;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Mvcpro1-20141022154431.mdf" providerName="System.Data.SqlClient" />-->    <!--<add name="hour1Entities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=hour1;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />    <add name="hourEntities" connectionString="metadata=res://*/Models.Hour.csdl|res://*/Models.Hour.ssdl|res://*/Models.Hour.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=hour1;user id=sa;password=123456;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />-->    <add name="complaintSysDBEntities" connectionString="metadata=res://*/complaintSys.csdl|res://*/complaintSys.ssdl|res://*/complaintSys.msl;provider=System.Data.SqlClient;provider connection string="data source=114.80.154.68,32433;initial catalog=complaintSysDB;user id=sa;password=12344321;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>  </connectionStrings>


 



ASP.NET 項目總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.