Advertisement
chooksz

C#

Jul 23rd, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8. using System.IO;
  9. using System.Data;
  10.  
  11. namespace MVCtest.Controllers
  12. {
  13.     public class HomeController : Controller
  14.     {
  15.         public ActionResult Index()
  16.         {
  17.             string path = Server.MapPath("~/XML/top.xml");
  18.             XmlDocument xDoc = new XmlDocument();
  19.             xDoc.Load(path);
  20.             string xmlContent = xDoc.InnerXml;
  21.             var deXml = horseracing.Deserialize(xmlContent);
  22.  
  23.             //racename
  24.             var rName = deXml.name;
  25.             ViewBag.racename = rName;
  26.  
  27.             //meeting name
  28.             var mName = from m in deXml.meetings
  29.                         select m;
  30.             ViewBag.race = mName;
  31.  
  32.             var time = from n in mName.Select(t => t.marketgroups)
  33.                        select n.Select(t => t.tsstart);
  34.  
  35.             List<string> min = new List<string>();
  36.  
  37.             foreach (var nxt in time)
  38.             {
  39.                 min.AddRange(nxt);
  40.             }
  41.  
  42.             List<DateTime> nxt5races = new List<DateTime>();
  43.  
  44.             foreach (var q in min)
  45.             {
  46.                 //DateTime minTime = new DateTime(2013, 3, 22);
  47.                 //TimeSpan ts = new TimeSpan(13, 25, 00);
  48.                 //minTime = minTime.Date + ts;
  49.                 DateTime minTime;
  50.  
  51.                 DateTime date = DateTime.Parse(q);
  52.                 date.ToShortTimeString();
  53.                 minTime = date;
  54.             }
  55.  
  56.  
  57.             var defaultXML = from x in mName.Select(i => i.marketgroups[0].idfwmarketgroup)
  58.                              select x;
  59.             foreach (string marketid in defaultXML)
  60.             {
  61.                 marketid.ToString();
  62.                 ajax_getMarketGroup(marketid);
  63.                 break;
  64.             }
  65.  
  66.             return View();
  67.  
  68.         }
  69.  
  70.  
  71.         public ActionResult ajax_getMarketGroup(string marketid)
  72.         {
  73.  
  74.             string path = Server.MapPath("~/XML/marketgroup" + "//" + marketid + ".xml");
  75.             XmlDocument doc = new XmlDocument();
  76.             try
  77.             {
  78.                 doc.Load(path);
  79.                 string xmlContent = doc.InnerXml;
  80.                 var deXml = marketgroup.Deserialize(xmlContent);
  81.  
  82.                 var Venue = deXml.markets.Select(v => v.venue).FirstOrDefault();
  83.                 ViewBag.Venue = Venue;
  84.  
  85.                 var sportName = deXml.markets.Select(s => s.sportname).FirstOrDefault();
  86.                 ViewBag.sportName = sportName;
  87.  
  88.                 var Moment = deXml.markets.Select(m => m.tsstart).FirstOrDefault();
  89.                 ViewBag.Moment = Convert.ToDateTime(Moment).ToShortTimeString();
  90.  
  91.                 var raceLength = deXml.markets.Select(r => r.racelength).FirstOrDefault();
  92.                 ViewBag.raceLength = raceLength;
  93.  
  94.                 var selections = (from s in deXml.markets.Select(s => s.selections)
  95.                                   select s).FirstOrDefault()
  96.                                  .OrderByDescending(n => n.competitornumber.PadLeft(2, '0'));
  97.                 ViewBag.selections = selections;
  98.             }
  99.             catch (Exception)
  100.             {
  101.  
  102.             }
  103.             return PartialView("RaceCard");
  104.         }
  105.         }
  106.  
  107.      
  108.     }
  109. ------------------------------------------------------HomeController-------------------------------------------------------------------
  110.  
  111. ------------------------------------------------------XmlSerialization-----------------------------------------------------------------
  112. using System;
  113. using System.Collections.Generic;
  114. using System.Linq;
  115. using System.Web;
  116. using System.Text;
  117. using System.Xml.Serialization;
  118. using System.IO;
  119.  
  120.  
  121. [Serializable()]
  122. public abstract class XmlSerialization<T>
  123. {
  124.  
  125.     /// Must have default constructor for xml serialization
  126.  
  127.     public XmlSerialization()
  128.     {
  129.     }
  130.  
  131.     /// Create an xml representation of this instance
  132.  
  133.     public string Serialize()
  134.     {
  135.         XmlSerializer serializer = new XmlSerializer(this.GetType());
  136.         using (StringWriter stream = new StringWriter())
  137.         {
  138.             serializer.Serialize(stream, this);
  139.             stream.Flush();
  140.             return stream.ToString();
  141.         }
  142.     }
  143.  
  144.     /// Creata a new instance from an xml string.
  145.  
  146.     /// The client is responsible for deserialization of the correct type
  147.  
  148.     public static T Deserialize(string xml)
  149.     {
  150.         if (string.IsNullOrEmpty(xml))
  151.         {
  152.             throw new ArgumentNullException("xml");
  153.         }
  154.         var serializer = new XmlSerializer(typeof(T));
  155.         using (var stream = new StringReader(xml.Replace("&", " ")))
  156.         {
  157.             try
  158.             {
  159.                 return (T)serializer.Deserialize(stream);
  160.             }
  161.             catch (Exception ex)
  162.             {
  163.                 // The serialization error messages are cryptic at best.
  164.                 // Give a hint at what happened
  165.                 throw new InvalidOperationException("Failed to " +
  166.                                  "create object from xml string", ex);
  167.             }
  168.         }
  169.     }
  170. }
  171. ---------------------------------------------------------------------------------------------------------------------------------------
  172.  
  173. -----------------------------------------------------------Index.cshtml----------------------------------------------------------------
  174.  
  175. @using MVCtest.Controllers
  176.  
  177. <script src="~/Scripts/jquery-1.8.2.min.js"></script>
  178. <link href="~/Scripts/StyleSheet1.css" rel="stylesheet" />
  179.  
  180.  
  181. <script>
  182.    $(document).ready(function () {
  183.         $(".marketTime").click(function (event) {
  184.             var marketid = event.target.id;
  185.             //alert(marketid);
  186.             $("#raceCard").addClass("raceCard");
  187.             $(".marketTime.selected").removeClass("selected");
  188.             $(this).addClass("selected");
  189.             $.ajax({
  190.                 url: "@Url.Action("ajax_getMarketGroup", "Home")",
  191.                 data: { marketid: marketid },
  192.                 type: "GET",
  193.                 success: function (data) {
  194.                     $("#raceCard").html(data);
  195.                 }
  196.             });
  197.         });
  198.  
  199.     });
  200. </script>
  201.  
  202.  
  203. <h2>Horse Racing</h2>
  204. <b>Race Name: </b> @ViewBag.racename
  205. <br /> <hr />
  206. <table>
  207.  
  208.     <tbody>
  209.         <tr>
  210.             <td>
  211.                 <b>Next 5 races</b>
  212.             </td>
  213.  
  214.         </tr>
  215.         @foreach (var m in ViewBag.race)
  216.         {
  217.             <tr>
  218.                 <td>
  219.                     <b>@m.name</b>
  220.                 </td>
  221.                 @foreach (var market in @m.marketgroups)
  222.                 {
  223.                     <td>
  224.                         <a href="#" id="@market.idfwmarketgroup" class="marketTime">
  225.                             @Convert.ToDateTime(market.tsstart).ToShortTimeString()
  226.                         </a>
  227.                     </td>
  228.                 }
  229.             </tr>
  230.         }
  231.     </tbody>
  232. </table>
  233. <br />
  234. <div id="raceCard" style="width: 30%">
  235. </div>
  236.  
  237. -----------------------------------------------------------------Index.cshtml^----------------------------------------------------------
  238.  
  239.  
  240. ----------------------------------------------------------------RaceCard.cshtml--------------------------------------------------------
  241.  
  242.  
  243. <h4 style=" text-align: center">
  244.     @ViewBag.Venue - @ViewBag.Moment - @ViewBag.raceLength -  @ViewBag.sportName
  245. </h4>
  246. <table id="raceTable" style="width: 100%">
  247.     <thead>
  248.         <tr>
  249.             <th>No.</th>
  250.             <th>Horse</th>
  251.             <th>Jockey</th>
  252.             <th>Odds</th>
  253.         </tr>
  254.     </thead>
  255.     <tbody>
  256.         @{
  257.             try
  258.             {
  259.                 foreach (var info in ViewBag.selections)
  260.                 {
  261.                     <tr>
  262.                         <td style=" text-align: center">
  263.                             @info.competitornumber
  264.                         </td>
  265.                         <td style=" text-align: center">
  266.                             @info.name<br />
  267.                             <span style="font-size: 11px">Form: @info.selectionhashcode</span>
  268.                         </td>
  269.                         <td style=" text-align: center">
  270.                             @info.jockeyname
  271.                         </td>
  272.  
  273.                         @if (@Convert.ToBoolean(info.is1stfavourite) == true || @Convert.ToBoolean(info.is2ndfavourite) == true)
  274.                         {
  275.                             <td style=" text-align: center">SP</td>
  276.                         }
  277.                         else if (@info.currentpriceup == string.Empty || @info.currentpricedown == string.Empty)
  278.                         {
  279.                             <td style=" text-align: center">
  280.                                 0 / 0
  281.                             </td>
  282.                         }
  283.                         else
  284.                         {
  285.                             <td style=" text-align: center">
  286.                                 @info.currentpriceup / @info.currentpricedown
  287.                             </td>
  288.                         }
  289.                     </tr>
  290.                 }
  291.             }
  292.             catch (Exception ex)
  293.             {
  294.                 <script type="text/javascript">
  295.                     alert("XML File not found!");
  296.                 </script>
  297.             }
  298.         }
  299.     </tbody>
  300. </table>
  301. ---------------------------------------------------------------------------------------------------------------------------------------
  302. CSS
  303. .selected {
  304.     background-color: lightblue;
  305. }
  306.  
  307. tr {
  308.   border-bottom: solid 1px black;
  309. }
  310.  
  311. a {
  312.     text-decoration: none;
  313. }
  314.  
  315. .raceCard {
  316. border: solid black 1px;
  317. }
  318.  
  319. a:hover {
  320.     background-color: lightblue;
  321. }
  322.  
  323. tr:nth-child(odd)       { background-color:#eee; }
  324. tr:nth-child(even)      { background-color:#fff; }
  325. --------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement