First, let's look at the view.Code:
<Div class = "show_h1" id = "divhomepagebestsellers"> </div> $ (document ). ready (function () {// use ajax to display the preceding HTML code $. ajax ({cache: false, type: "Post", URL: "@ (URL. routeurl ("homepagebestsellersjson") ", // The route has defined data: {" productthumbpicturesize ": 75}, success: function (data) {$ ("# divhomepagebestsellers" ).html(data.html) ;}, error: function (xhr, ajaxoptions, thrownerror ){}});});
Ajax is used to call the action of homepagebestsellersjson. Let's take a look at its code.
[Httppost] public actionresult homepagebestsellersjson (Int? Productthumbpicturesize, Int? Orderby, Int? Categoryid) {If (! _ Catalogsettings. showbestsellersonhomepage | _ catalogsettings. response = 0) return JSON (New {html = ""}); // This step only shows the judgment, regardless of VaR model = gethomepagebestsellersmodel (productthumbpicturesize, orderby, categoryid ); // This is to get model, return JSON (New {html = This. renderpartialviewtostring ("homepagebestsellers", model ),});}
Let's take a look at the view code of homepagebestsellersjson:
@ Model homepagebestsellersmodel @ using NOP. web. models. catalog; @ if (model. products. count> 0) {int Index = 0; foreach (VAR item in model. products) {<DL> <DT> <Div class = "ranks"> @ (++ index) </div> <a href = "@ URL. routeurl ("product", new {productid = item. id}) "> </a> </DT> <DD> <S> @ item. productprice. oldprice </S> <br/> <I class = "price"> @ item. productprice. price </I> <br/> <a href = "@ URL. routeurl ("product", new {productid = item. id}) "Title =" @ item. defaultpicturemodel. title "> @ item. name </a> </DD> </dl> }}
Next I will focus on how to implement this.
Return JSON (New {html = This. renderpartialviewtostring ("homepagebestsellers", model ),});
Let's take a look at the renderpartialviewtostring method. The following Code shows that it is the expansion method of controller:
Public static class contollerextensions {public static string renderpartialviewtostring (this controller, string viewname, Object Model) {// Original source code: http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/ if (string. isnullorempty (viewname) viewname = controller. controllercontext. routedata. getrequiredstring ("action"); controller. viewdata. model = model; // input model data using (VAR Sw = new stringwriter () {viewengineresult viewresult = system. web. MVC. viewengines. engines. findpartialview (controller. controllercontext, viewname); // In this step, find view var viewcontext = new viewcontext (controller. controllercontext, viewresult. view, controller. viewdata, controller. tempdata, SW);
// This step transfers the model data in the Controller to the view and passes the viewresult. view. render generates the string code to complete the Ajax call and implementation.
Viewresult. View. Render (viewcontext, SW); Return Sw. getstringbuilder (). tostring ();}}}
Let's take a look at the inheritance relationship between the implementation of system. Web. MVC. viewengines. Engines. findpartialview and the class (themeablevirtualpathproviderviewengine) to which the method belongs,
Public abstract class themeablevirtualpathproviderviewengine: virtualpathproviderviewengine
Public override viewengineresult findpartialview (controllercontext, string partialviewname, bool usecache) {var mobiledevicehelper = enginecontext. current. resolve <imobiledevicehelper> (); bool usemobiledevice = mobiledevicehelper. ismobiledevice (controllercontext. httpcontext) & mobiledevicehelper. mobiledevicessupported ()&&! Mobiledevicehelper. customerdontusemobileversion (); string overrideviewname = usemobiledevice? String. format ("{0 }. {1} ", partialviewname, _ mobileviewmodifier): partialviewname; viewengineresult result = findthemeablepartialview (controllercontext, overrideviewname, usecache, usemobiledevice ); // if we're looking for a mobile view and Couldn't find it try again without modifying the viewname if (usemobiledevice & (result = NULL | result. view = NULL) Result = findthemeablepartialview (controllercontext, partialviewname, usecache, false); return result ;}
Protected virtual viewengineresult findthemeablepartialview (controllercontext, string partialviewname, bool usecache, bool mobile) {string [] strarray; If (controllercontext = NULL) {Throw new argumentnullexception ("controllercontext");} If (string. isnullorempty (partialviewname) {Throw new argumentexception ("partial view name cannot be null or empty. "," partialviewname ");} V Ar theme = getcurrenttheme (mobile); string requiredstring = controllercontext. routedata. getrequiredstring ("controller"); string str2 = This. getpath (controllercontext, this. partialviewlocationformats, this. areapartialviewlocationformats, "partialviewlocationformats", partialviewname, requiredstring, theme, "partial", usecache, mobile, out strarray); If (string. isnullorempty (str2) {return New View Engineresult (strarray);} return New viewengineresult (this. createpartialview (controllercontext, str2), this);} protected virtual string getpath (controllercontext, string [] locations, string [] arealocations, string locationspropertyname, string name, string controllername, string theme, string cachekeyprefix, bool usecache, bool mobile, out string [] searchedlocations) {searchedl Ocations = _ emptylocations; If (string. isnullorempty (name) {return string. empty;} string areaname = getareaname (controllercontext. routedata); // little hack to get NOP's admin area to be in/Administration/instead of/NOP/admin/or areas/admin/If (! String. isnullorempty (areaname) & areaname. equals ("admin", stringcomparison. invariantcultureignorecase) {// admin area does not support mobile devices if (mobile) {searchedlocations = new string [0]; return string. empty;} var newlocations = arealocations. tolist (); newlocations. insert (0 ,"~ /Administration/views/{1}/{0}. cshtml "); newlocations. insert (0 ,"~ /Administration/views/{1}/{0}. vbhtml "); newlocations. insert (0 ,"~ /Administration/views/shared/{0}. cshtml "); newlocations. insert (0 ,"~ /Administration/views/shared/{0}. vbhtml "); arealocations = newlocations. toarray ();} bool flag =! String. isnullorempty (areaname); List <viewlocation> viewlocations = getviewlocations (locations, flag? Arealocations: NULL); If (viewlocations. count = 0) {Throw new invalidoperationexception (string. format (cultureinfo. currentculture, "properties cannot be null or empty. ", new object [] {locationspropertyname});} bool flag2 = isspecificpath (name); string key = This. createcachekey (cachekeyprefix, name, flag2? String. Empty: controllername, areaname, theme); If (usecache) {var cached = This. viewlocationcache. getviewlocation (controllercontext. httpcontext, key); If (cached! = NULL) {return cached;} If (! Flag2) {return this. getpathfromgeneralname (controllercontext, viewlocations, name, controllername, areaname, theme, key, ref searchedlocations);} return this. getpathfromspecificname (controllercontext, name, key, ref searchedlocations );}
some specific methods have not been provided. I hope you will have a great deal to learn about the NOP source code. Maybe I am not talking about cleaning much, but I hope you can understand the general process. This function is very useful to us. Amen!