ASP.NET MVC 5 View ( Razor ) + jQuery Form Post + Create Session
\Models\Player.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApp.Models { [Serializable] public class Player { public String PlayerName; public String PlayerNation; public String PlayerTeam; } } |
\Views\Home\Form.cshtml |
@model List<DAO.PLAYER_VIEW> … … <script language="javascript" type="text/javascript"> $(document).ready(function () { $(".btn").click(function () { Name = getName($(this).attr("id")); Nation = getNation($(this).attr("id")); Team = getTeam($(this).attr("id")); $.post("/Home/SessionBuild", { Name:Name, Nation:Nation, Team:Team }).done(function (data) { window.location = "/"; }); }); }); function getName(btnID) { ID = "#name" + btnID.replace("btn", ""); return $(ID).html() } function getNation(btnID) { ID = "#nation" + btnID.replace("btn", ""); return $(ID).html() } function getTeam(btnID) { ID = "#team" + btnID.replace("btn", ""); return $(ID).html() } </script> <table border="0" cellpadding="0" cellspacing="0" width="100%"> @{ int i = 1; foreach (DAO.PLAYER_VIEW item in Model) { <tr> <td><input type="button" class="btn" id="btn@(i.ToString())" value="Select"></td> <td><span id="name@(i.ToString())">@(item.PLAYER_NAME)</span></td> <td><span id="nation@(i.ToString())">@(item.NATION)</span></td> <td><span id="team@(i.ToString())">@(item.TEAM)</span></td> </tr> i++; } } </table> |
\Views\Home\Home.cshtml |
<div class="jumbotron"> @ViewBag.PlayerName<br /> @ViewBag.PlayerNation<br /> @ViewBag.PlayerTeam </div> |
\Views\Home\About.cshtml |
<div class="jumbotron"> @ViewBag.PlayerName<br /> @ViewBag.PlayerNation<br /> @ViewBag.PlayerTeam </div> |
\Controllers\HomeController.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using DAO; using System.Web.SessionState; namespace WebApp.Controllers { public class HomeController : Controller { public ActionResult Index() { if (System.Web.HttpContext.Current.Session["Player"] != null) { Models.Player p = System.Web.HttpContext.Current.Session["Player"] as Models.Player; ViewBag.PlayerName = p.PlayerName; ViewBag.PlayerNation = p.PlayerNation; ViewBag.PlayerTeam = p.PlayerTeam; } return View(); } public ActionResult About() { if (System.Web.HttpContext.Current.Session["Player"] != null) { Models.Player p = System.Web.HttpContext.Current.Session["Player"] as Models.Player; ViewBag.PlayerName = p.PlayerName; ViewBag.PlayerNation = p.PlayerNation; ViewBag.PlayerTeam = p.PlayerTeam; } return View(); } … … public ActionResult Form() { SoccerEntities entity = new SoccerEntities(); List<PLAYER_VIEW> list = entity.PLAYER_VIEW.ToList(); return View(list); } public ActionResult SessionBuild(FormCollection collection) { Models.Player p = new Models.Player(); p.PlayerName = collection["Name"].ToString(); p.PlayerNation = collection["Nation"].ToString(); p.PlayerTeam = collection["Team"].ToString(); System.Web.HttpContext.Current.Session["Player"] = p; return View("Index"); } } } |