Asp.net mvc background operation-read/write xml control homepage dynamic page switch display, mvcxml
I. background
In the asp.net mvc project, you need to develop several activity la S and display them on the right side of the homepage in the form of a sidebar. The time of several activities is inconsistent, to prevent visitors from entering the service pages that are not open during the activity. Therefore, you must not only add the restriction function to the activity code, but also cancel the display of outdated pages on the foreground. Background:
Ii. Problem Analysis
To control the page switch display, you must have a media for storing the switch information and record the operations generated in the background, when the browser loads the home page, it can read operation records and control the page switch. Therefore, two types of media storage switch are used to record information: mysql database and xml. Considering that this is a little useful for database functions and will increase the consumption of database resources when loading the homepage, it is better to use xml documents to record switch records. Here we need to do some preliminary work, first design the xml structure:
<?xml version="1.0" encoding="gb2312"?><Settings> <Setting ID="1"> <title>****</title> <status>true</status> </Setting></Settings>
Note: The ID indicates the setting tag corresponding to multiple records. The title indicates the Page name, that is, the service name. The status indicates the status of the current service page, there are two values: true or false. Add a folder named Xml in the project and save the design XML named ServiceSettings. xml.
3. solution or process
FirstTo generate the switch operation requires the control on the page. the checkbox label is used here, of course, this is not the only choice. Friends can use other labels as the input control. The idea is: I touch the checkbox, the corresponding js Code record value, and upload it to the background action using ajax, and the background action responds.
Js Code:
function BloodController() { $.ajax({ type: "POST", url: "/Admin/Controller", data: {}, dataType: "Json" }); }
Html code:
<input name="switch-field-1" class="ace ace-switch ace-switch-6" type="checkbox" onclick="Controller()" />
The data in ajax is not marked here. When the background action is triggered once, the code for operating the xml in the action will be run, and the value recorded in the xml will be changed.
Then,Is the implementation of xml operations in action. The idea is: first obtain the relative position of xml in the application, and then change the xml value.
public string Xmlpath = HttpRuntime.AppDomainAppPath.ToString() + "/Xml/ServiceSettings.xml"; public ActionResult Controller() { OperateXml sx = new OperateXml(); sx.modifyXml(Xmlpath); return Json(""); }
Note: HttpRuntime. AppDomainAppPath. ToString (); you can obtain the root directory of the current application. To operate multiple xml files, I wrote OperateXml as a class method. Here I will go to the xml operation details.
Not much about the principles of xml operations, in fact, ado.net also supports great, the structure of xml documents is similar to html, but it has a higher degree of freedom and a higher mark. Not to mention, go directly to the code and explain it in the comments:
Public void modifyXml (string Path) {XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load (Path); XmlNodeList nodeList = xmlDoc. selectSingleNode ("Settings "). childNodes; // obtain all subnodes of the bookstore node foreach (XmlNode xn in nodeList) // traverse all subnodes {XmlElement xe = (XmlElement) xn; // convert the subnode type to the XmlElement type if (xe. getAttribute ("ID") = "1") {XmlNodeList nls = xe. childNodes; // continue to obtain the foreach (XmlNode xn1 in nls) of all the child nodes of xe) // Traverse {XmlElement xe2 = (XmlElement) xn1; // The Conversion Type if (xe2.Name = "status") // if {if (xe2.InnerText! = "False") {xe2.InnerText = "false"; // modify the break. // you can find and exit.} else if (xe2.InnerText = "false ") {xe2.InnerText = "true"; // The break is modified. // you can find and exit.} break ;}} xmlDoc. save (Path); // Save.}
Because the comments in the Code are clear enough, we will not repeat them here. Note that the status here only has two values: true or false. The two values are read at the front end, for page control.
Next,Is the value in the xml read from the foreground. The idea is: Read the xml value in the dynamic page and make a judgment. If it is true, the page is displayed, and if it is false, the page is not displayed. Read the xml code and describe it in the annotations. It should be noted that in order to control the display of multiple pages, the List <string> is used to reprint the values of multiple statuses.
public List<string> getAllXml(string Path) { string status = ""; XmlDocument doc = new XmlDocument(); doc.Load(Path); XmlNode xn = doc.SelectSingleNode("Settings"); XmlNodeList xnl = null; xnl = xn.SelectNodes("/Settings/Setting/status"); List<string> list = new List<string>(); foreach (XmlNode xn1 in xnl) { status = xn1.InnerText; list.Add(status); } return list; }
It should be noted that in order to control the display of multiple pages, the List <string> is used to reprint the values of multiple statuses.On the front-end page, we can achieve this:
@ Using TheOne. tool @ {OperateXml sx = new OperateXml (); List <string> list = new List <string> (); string path = HttpRuntime. appDomainAppPath. toString () + "/Xml/QuickServiceSettings. xml "; list = sx. getAllXml (path); string status_Umbrella = list [0]. toString (); // string status_Bottle = list [1]. toString (); // string status_Older = list [2]. toString (); // string status_Blood = list [3]. toString () ;}@ if (status_Umbrella = "true") {<div class = "blog-grids wow fadeInDown" data-wow-duration = ". 8 s "data-wow-delay = ". 2 s "> <div class =" blog-grid-left "> <a href ="/UmbrellaManage/CreateUmbrellaUser "> </a> </div> <div class =" blog-grid-right ">
Here, the introduction of the xml control page switch is basically over.
Iv. Summary
The core of the idea is to operate xml, modify xml, and read xml. For how to implement the checkbox status switch, this implementation can be operated using js, read the status value, and let the corresponding js code control whether the checkbox status is selected or not.Actual Project demo address: ClickNannong Industry Committee
If my ideas and methods are incorrect, please correct me and ask for advice with an empty mind.Reprinted, please indicate the source and source. Thank you for your cooperation.