VS2010のWebプロゞェクトの䟋

私にずっおのVS 2010の出力は、たず第䞀に、.Net 4、Entity Framework 4、ASP.NET MVC 2で動䜜する胜力です。
私の意芋では、埗られたすべおの理論的知識は実際の経隓で衚珟されるべきです。 そのため、機䌚が䞎えられたらすぐに、VS 2010を䜿甚しおプロゞェクトを実装したした。これで、新しい機胜の印象を共有する準備が敎いたした。
この蚘事は、高床な開発者向けに蚭蚈されおいたす



少し前のむンタビュヌの1぀で、VS 2010を䜿甚しお行うこずにしたタスクが提䟛されたした。 条件ではそうでなければ同意されたせんでした。 私にずっおこの仕事は、たず第䞀に、提䟛された機䌚に慣れる良い機䌚でした。
条件に぀いお簡単に説明したす。MSSQL Server NorthWindデヌタベヌスの Customersテヌブルのデヌタを怜玢および衚瀺するためのWebアプリケヌションを䜜成したす 。 怜玢は、囜囜、垂垂、䌚瀟名䌚瀟名の基準に埓っお実行する必芁がありたす。 怜玢語は郚分的に指定できるこずに泚意しおください。

䜕よりも、ナヌザヌフレンドリヌな゜リュヌションを䜜成するのが奜きです。 私の意芋では、Web䞊のナヌザヌにずっお最適な゜リュヌションはajaxです。 ぀たり、ペヌゞをリロヌドするこずなく、サむトの仕事。 䜜成する目暙ずしお蚭定したのは、このむンタヌフェむスです。

郚品の遞択


私は、垞に仕事をしおいるツヌルから自分が䞀番奜きなものを遞ぶこずにしたした。
1぀目はjQueryです。 倚くの堎合、リッチなむンタヌフェむスを開発する必芁があり、このフレヌムワヌクが倧奜きだからです。 圌がVSに入る前の長い間、そしお圌の倧衆化。 圌の柔軟性のおかげで、あなたはゞェダむのように感じたす
第二に、それはEntity Framework 4です。私は、バヌゞョン3.5からそれほど前ではないこずに慣れおいたす。 MSは開発者向けにEFを掚奚しおいたす。EF開発者のブログを読むず、EF 4が期埅されおいるものであるず確信したした。 最埌に、アダプタヌを日付オブゞェクトから分離する組み蟌み機胜がありたす。 これに぀いおは埌で説明したす。Pocoはt4テンプレヌトに基づいおオブゞェクトを生成するこずを意味したす。 そしお、Linq to Entityの存圚は、このフレヌムワヌクに同じ軜さの感芚を远加したす。 私にラむトセヌバヌ 
第䞉に、ASP.NET MVC 2はアプリケヌションの基盀です。 圌女がそこにいなかったずき、私は即興のツヌル曞き換えパス、クラむアントビュヌ、Webサヌビス圢匏のコントロヌラヌなどの助けを借りお非垞に䌌たようなこずを行い、MSからの解決策が出おきたずきに喜びを捚おたした。 リリヌス以来、mvcを䜿甚できたのは幞運でした。 これもたた私の愛です
最埌に、ajaxに぀いおは、Webサヌビスを䜿甚するこずにしたした。 実際のずころ、私はりェブがシステムの䞀郚にすぎない異皮アプリケヌションの開発に忙しく、ただ他の゜フトりェアがたくさんありたす。 はい、WCFをほのめかしおいたす WebサヌビスはDTOを亀換するため、それらにデヌタ属性を远加し、それらからサヌビスコントラクトを䜜成できたす。他のナヌザヌはアプリケヌションを䜿甚できたす。 たあ、かわいいでしょ 

ゞグ゜ヌパズル


アヌキテクチャに関しおは、䞀般に受け入れられおいる暙準を順守しおいたす。 この点に関しおMSが明確にその芋解を衚明したこずを非垞に嬉しく思いたす。 私たちのプロゞェクトで芚えおおくべきこず


レむダヌずは


デヌタ転送オブゞェクトDTOを䜿甚しお互いに通信したす
習慣がありたす-垞に衚瀺から始めたす

ビュヌを䜜成する


Index.aspxを倉曎したした
< form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  1. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  2. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  3. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  4. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  5. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  6. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  7. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  8. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  9. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  10. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  11. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  12. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  13. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  14. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  15. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  16. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  17. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  18. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  19. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  20. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  21. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
  22. < form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .
< form id ="customer-search-form" action ="/" enctype ="application/x-www-form-urlencoded" method ="get" > < label > Company Name < input name ="name" /></ label > < label > City < input name ="city" /></ label > < label > Country < input name ="country" /></ label > < input type ="submit" value ="search" /> </ form > < div id ="customer-search-result" > < table > < thead > < tr > < th class ="CustomerID" > id </ th > < th class ="CompanyName" > name </ th > < th class ="ContactTitle" > contract </ th > < th class ="City" > city </ th > < th class ="Country" > country </ th > < th class ="Phone" > phone </ th > < th class ="Fax" > fax </ th > </ tr > </ thead > < tbody ></ tbody > </ table > </ div > * This source code was highlighted with Source Code Highlighter .

ここではおそらくすべおが明らかです。 thではクラスの名前のみを説明したす。 これらは、遞択を衚瀺するためのタグずしお䜿甚されたす。 プロゞェクトでは、jQueryにいく぀かのプラグむンを远加するこずにしたした。


Site.Masterの接続枈みプラグむンぞのリンクを远加し、jsファむルを远加したす。これをcustomer-list.jsず呌びたす

Javascript


私はクロヌゞャヌが䜕であるかを知っおいるので、最初ず最埌に远加しおください

( function (){

$( function (){

//.... ...

});

})();


* This source code was highlighted with Source Code Highlighter .

同時にダりンロヌドコヌドの開始を远加したす。 なぜなら 1぀のビュヌがあり、jQueryプラグむンを実行したせんでした
Webサヌビスに接続するようにAjaxを構成したす。

$.ajaxSetup({
type: 'post' ,
contentType: 'application/json; charset=utf-8' ,
dataType: "json"
});


* This source code was highlighted with Source Code Highlighter .

䜿甚するものを芋぀けたす。

var $result = $( '#customer-search-result' );
var $form = $( '#customer-search-form' );


* This source code was highlighted with Source Code Highlighter .

質問は、おそらく、倉数の先頭に$を匕き起こしたすPHPはそれずは䜕の関係もありたせんjQueryでラップされた倉数を呌び出しお、すぐに芋えるようにしたす。 そしお、圌らに䜕を期埅するかは明らかです。
次に、 オヌトコンプリヌトを構成したす。 なぜなら 3぀のフィヌルドず3぀の察応するサヌビスがありたす。蚭定を1぀のオブゞェクトに結合しおから、次を䜿甚する方が簡単です。

var paramsAutocomplete = {
name: '' ,
queryParams: function (info){
var res = {};

res[ this .name] = info.q;

return $.toJSON(res);
},
formatItem: function (row, i, max, term) {
return row.replace( new RegExp( "(" + term + ")" , "gi" ), '$1' );
},
parse: function (data) {
var parsed = [];

$.each(data.d, function (){
parsed.push({data: this , value: this , result: this });
});

return parsed;
}
};


* This source code was highlighted with Source Code Highlighter .

暙準の配信ではqueryParams関数に泚意する䟡倀がありたすが、 オヌトコンプリヌトではこのようなパラメヌタヌの倉曎は蚱可されおいたせん。 このコヌドが機胜するために、このプラグむンをわずかに倉曎したした。もちろん、デフォルトで機胜するオプションを残したした。

data: options.queryParams ? options.queryParams({
q: lastWord(term),
limit: options.max
}) : $.extend({
q: lastWord(term),
limit: options.max
}, extraParams),


* This source code was highlighted with Source Code Highlighter .

Ajaxリク゚ストが発生する348行のように芋えたす。 これは、Webサヌビスずの互換性のために行われたす。

オヌトコンプリヌトを接続したす

var $name = $form.find( '[name=name]' )
.autocomplete(
'Services/Customers.asmx/CompanyNames' ,
$.extend(paramsAutocomplete, {name: 'name' }));

var $city = $form.find( '[name=city]' )
.autocomplete(
'Services/Customers.asmx/Cities' ,
$.extend(paramsAutocomplete, {name: 'city' }));

var $country = $form.find( '[name=country]' )
.autocomplete(
'Services/Customers.asmx/Countries' ,
$.extend(paramsAutocomplete, {name: 'country' }));


* This source code was highlighted with Source Code Highlighter .

衚瀺するフィヌルドを収集したす。

var $table = $result.find( 'table' );

var labels = ( function (){
var res = [];
$table.find( 'thead th' ).each( function ()
{
res.push($( this ).attr( 'class' ));
});
return res;
})();


* This source code was highlighted with Source Code Highlighter .

次に、デヌタ゜ヌスであるWebサヌビスず連携するようにDataTableを構成する必芁がありたす。

var lastLength = 10;

var dataTable = $table.dataTable({
bProcessing: true ,
bServerSide: true ,
sPaginationType: 'full_numbers' ,
sAjaxSource: 'Services/Customers.asmx/Search' ,
fnServerData: function ( sSource, aoData, fnCallback ) {

// aoData
var findByLabel = function (o, name)
{
var res = null , find = false ;
$(o).each( function (){
if (!find && this .name === name)
{
find = true ;
res = this .value;
}
});
return res;
};

// dataTable'
var convert = function (list)
{
var res = [];

$(list).each( function (){
var item = [];
var row = this ;

$(labels).each( function ()
{
item.push(row[ this ]);
});

res.push(item);
});
return res;
};

var onPage = aoData ? findByLabel(aoData, 'iDisplayLength' ) : lastLength;
var start = aoData ? findByLabel(aoData, 'iDisplayStart' ) : 0;
var sortField = aoData ? findByLabel(aoData, 'iSortCol_0' ) : 0;
var sortIsAsc = aoData ? findByLabel(aoData, 'sSortDir_0' ) === 'asc' : true ;

lastLength = onPage;

//
//
$.ajax( {
url: sSource,
data: $.toJSON({
name: $name.val(),
city: $city.val(),
country: $country.val(),
order: {
Field: labels[sortField],
Dir: sortIsAsc ? 'Ascending' : 'Descending'
},
start: start,
onPage: onPage
}),
success: function (data){

fnCallback({
iTotalRecords: data.d.Count,
iTotalDisplayRecords: data.d.Count,
aaData: convert(data.d.List)
});
}
} );
}
});


* This source code was highlighted with Source Code Highlighter .


私の意志であれば、このDataTableをたったく別の方法で蚘述しおいたした 。 圌は圌ず䞀緒に働くためにあたりにも倚くの動きを必芁ずし、それは重いです。 私はこのコヌドが䞀番奜きではなく、ずおもかさばっおいたす。 DataTableを䜿甚するず、内郚メカニズムを䜿甚しお同じ機胜を実装できたすが、コヌドの快適性はさらに䜎䞋するず思いたす。
送信凊理をフォヌムに远加したす

$form.submit( function (){

dataTable.fnDraw( true );

return false ;
});


* This source code was highlighted with Source Code Highlighter .

JSで完了です。 VSで最初に泚目できるこずは、JSコヌドで䜜業するこずが䟿利になったこずです。 理解できないむンデントによるゞャンプ、コヌド怜蚌によるブレヌキはもうありたせん。 jQueryヒントの存圚ず远加されたLint JSにより、VSは最高のJavaScriptコヌド゚ディタヌの1぀になりたす。

Webサヌビス


WebサヌビスCustomers.asmxを䜜成し、[ScriptService]のコメントを倖し、System.Web.Script.Servicesを䜿甚しお远加したす。
メ゜ッドを䜜成したす。


必芁な属性を远加したす。

[WebMethod]
[ScriptMethodResponseFormat = ResponseFormat.Json]

JSON圢匏での䜜業に必芁です。テキストデヌタをりェブに転送するのに適しおいたす。倚くの理由がありたす。より簡朔、読みやすく、解析が高速です。
最初の3぀のメ゜ッドは同じ方法で入力されたすが、そのうちの1぀だけの䟋を瀺したす。

using ( var context = new NorthwindEntities())
{
return context.Customers
.Where(c => c.CompanyName.Contains(name))
.Select(c => c.CompanyName)
.Distinct()
.OrderBy(n => n)
.ToArray();
}


* This source code was highlighted with Source Code Highlighter .

コンテキストを開き、芋぀けお、私たちに期埅されるこずを返したす。 Linq to Entitiesはこれをselectに倉換し、ToArrayを呌び出したずきにのみ実䜓化したす。 ずころで、なぜToArrayを特別に䜿甚しおいるのですか。 その理由は、このサヌビスをWCFで動䜜させるのが簡単になるからです。 なぜなら 配列はコントラクトで蚘述できたす。

より難しい怜玢

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Result<VS2010Lab.Customers> Search( string name, string city, string country, Sort order, int start, int onPage)
{
//
if (order == null || ! typeof (VS2010Lab.Customers).GetProperties().Any(f => f.Name == order.Field))
order = new Sort {Field = "CompanyName" , Dir = SortDirection.Ascending};

using ( var context = new NorthwindEntities())
{
//
var res = context.Customers
.Where(c => string .IsNullOrEmpty(name) || c.CompanyName.Contains(name))
.Where(c => string .IsNullOrEmpty(city) || c.City == city)
.Where(c => string .IsNullOrEmpty(country) || c.Country == country)
//
.Order(order);

// , DTO
return new Result<VS2010Lab.Customers>
{
// pagenator'
Count = res.Count(),
List = res.Paginate(
new ListFilter
{
Start = start,
Count = onPage
}
).ToArray()
};
}
}


* This source code was highlighted with Source Code Highlighter .

泚意すべきこず泚文、ペヌゞネヌション。 IQueryableの拡匵機胜ずしお説明したした。

public static IQueryable Order( this IQueryable query, Sort sort)
{
return sort.Dir == SortDirection.Ascending ?
query.Order(sort.Field) :
query.OrderDescending(sort.Field);
}

public static IQueryable Order( this IQueryable query, string name)
{
return query.ApplyOrder(name, "OrderBy" );
}

public static IQueryable OrderDescending( this IQueryable query, string name)
{
return query.ApplyOrder(name, "OrderByDescending" );
}

public static IQueryable Paginate( this IQueryable query, IListFilter filter)
{
return filter.Count > 0 ? query.Skip(filter.Start).Take(filter.Count) : query;
}


* This source code was highlighted with Source Code Highlighter .


Entity Framework 4


ADO.NET Entity Data Model edmxを䜜成し、そこにCustomersプレヌトを远加したす。

モデルの画面の癜い領域でマりスの右ボタンで「突く」ず「 コヌド生成項目の远加」を遞択したす

コヌド生成アむテムを远加

ここでは、VS 2010の別の機胜であるオンラむンテンプレヌトをダりンロヌドしたす。 開いお、 ADO.NET CPOCO Entity Generatorを遞択し、Northwind.ttなどの名前を入力しお、[ 远加 ] をクリックしたす
ADO.NET CPOCO Entity Generatorを远加したす
モデルプロパティF4に移動し、次を遞択したす。


これで、䞀連のデヌタオブゞェクトDOずそれらを操䜜するコンテキストができたした。 EF3.5のように、圌らは䞀緒に嘘を぀きたせん

結果

出来䞊がり 

結論


蚘事が倧量にありたしたが、VS2010だけでなく、このプロゞェクトはたったく異なりたすが、VS2010だけでなく、倚くの懞念を取り䞊げたせんでした。
私はただ孊生でしたが、VSのバヌゞョン5で䜜業を開始したした。 それ以来、スタゞオは倧きく倉わりたしたそしお、プログラマヌの品質ず䜿いやすさを改善するための泚意が適切なレベルにずどたっおいるこずを嬉しく思いたす。 たた、MSが最終的にWeb開発者に盎面したこずを願っおいたす。 IE5はHTML5のサポヌトを宣蚀したしたが、それはたったく別の話です。
レビュヌから意図的に逃したものは、ささいなこずを混乱させないために、プロゞェクト自䜓で芋るこずができたす。

ps蚘事が気に入ったら、投祚しおください。 コンテストに参加したす 

Source: https://habr.com/ru/post/J93331/


All Articles