<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<com.nowdocs.nowsource.Admin.ViewModels.SiteConfiguration.SALevelListViewModel>" %> <script type='text/javascript'> $(function () { $('li').click(function () { $('#SelectID').val($(this).children(':input').val()); $('ul li').removeClass("selected"); $(this).addClass("selected"); }); //方法一 $('#btn_del').click(function (e) { var sid = $('#SelectID').val(); if (sid != null && sid != '' && sid != '0') { e.preventDefault(); location.href = '<%= Url.Action("Delete","SalesAdminLevelNew")%>' + '/' + sid; //location.href = 'Delete/' + sid; } else $(this).val("0"); }); $('#btn_activate').click(function (e) { var sid = $('#SelectID').val(); if (sid != null && sid != '' && sid != '0') { e.preventDefault(); location.href = '<%= Url.Action("UpdateSalesAdminLevelActive","SalesAdminLevelNew")%>' + '/' + sid; //location.href = 'Delete/' + sid;方法二注掉了,有时候URL会拼写错误,不建议使用 } else $(this).val("0"); }); }); </script> <div id="group-list" class="admin-panel" style="width: 400px;"> <div class="title"> <%: com.nowdocs.nowsource.Admin.Resources.SalesAdminLevel.SALevelTitle %> </div> <div class="content"> <div class="wrap" id="ChildGroupList" style="display: block;"> <ul> <% if (Model.SALevelList != null) { foreach (var link in Model.SALevelList) { if (link.IsSelected) {%> <li class="selected"> <%} else { %> <li> <%} %> <a> <%: link.Text%></a><%: Html.Hidden("SalesAdminLevelID", link.intSalesAdminLevelID)%></li> <% } }%> </ul> </div> <div class="viewedit"> <%: Html.Hidden("SelectID")%> <!--方法三--> <input type="submit" id="btn_add" value="Add" onclick='this.form.action="<%= Url.Action("AddSetting") %>";' /> <!--方法四--> <input type="submit" id="btn_edit" value="Edit" onclick='this.form.action="<%= Url.Action("Edit1") %>";' /> <input type="button" id="btn_del" value="Delete" /> <input type="button" id="btn_activate" value="Activate/Deactivate" /> </div> </div> </div>
?Control.cs
using System; using System.Collections.Generic; using System.Web.Mvc; using com.nowdocs.nowsource.Admin.BusinessLogic.SiteConfiguration; using com.nowdocs.nowsource.Admin.ViewModels.SiteConfiguration; using com.nowdocs.nowsource.common; using com.nowdocs.nowsource.Models; using log4net; namespace com.nowdocs.nowsource.Admin.Controllers.SiteConfiguration { public class SalesAdminLevelNewController : AdminController { protected static readonly ILog logger = LogManager.GetLogger("SalesAdminLevelController"); public GroupModel CurrentGroup { get { return StateManager.CurrentGroup; } set { StateManager.CurrentGroup = value; } } public SalesAdminLevelNewController() : base() { } // // GET: /SalesAdminLevel/ public ActionResult Index() { return View(); } public ActionResult GetSALevelList(int id) { List<SalesAdminLevelModel> saLevelList = SalesAdminLevelBL.GetSalesAdminLevelList(); SALevelListViewModel savm = new SALevelListViewModel(); savm.SelectID = id; savm.SALevelList = new List<SALevelListItemViewModel>(); if (saLevelList.Count > 0) { foreach (var item in saLevelList) { SALevelListItemViewModel salm = new SALevelListItemViewModel(); salm.intSalesAdminLevelID = item.intSalesAdminLevelID; string isActive = item.blnSalesAdminLevelActive ? "" : "(Inactive)"; salm.Text = (item.strSalesAdminLevelName + isActive).Trim(); if (salm.intSalesAdminLevelID == id) { salm.IsSelected = true; } else { salm.IsSelected = false; } savm.SALevelList.Add(salm); } } //ViewData["ViewEdit"] = GetViewEdit(); ViewBag.SalesAdminLevelID = id; return View("SalesAdminLevelListNew", savm); } /// <summary> /// Display Edit View /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPost] public ActionResult EditView(UserTypeListViewModel model) { //var session = SessionManager.Current; CurrentGroup = null; if (model.ViewEdit.Equals("1")) return RedirectToAction("Add", new { id = 0 }); if (model.ViewEdit.Equals("2")) return RedirectToAction("Edit", new { id = model.SelectID }); if (model.ViewEdit.Equals("3")) return RedirectToAction("Delete", new { id = model.SelectID }); if (model.ViewEdit.Equals("4")) return RedirectToAction("UpdateSalesAdminLevelActive", new { id = model.SelectID }); return View(); } #region GetViewEdit() public static SelectList GetViewEdit() { List<SelectListItem> item = new List<SelectListItem>(); item.Add(new SelectListItem { Text = Resources.SalesAdminLevel.Select, Value = "0" }); item.Add(new SelectListItem { Text = Resources.SalesAdminLevel.AddSALevel, Value = "1" }); item.Add(new SelectListItem { Text = Resources.SalesAdminLevel.EditeSALevel, Value = "2" }); item.Add(new SelectListItem { Text = Resources.SalesAdminLevel.DeleteSALevel, Value = "3" }); item.Add(new SelectListItem { Text = Resources.SalesAdminLevel.ActivateOrDeactivate, Value = "4" }); return new SelectList(item, "Value", "Text"); } #endregion GetViewEdit() // // GET: /SalesAdminLevel/Create [HttpPost] [ActionName("AddSetting")] public ActionResult Add(SALevelListViewModel model) { SalesAdminLevelViewModel salvm = new SalesAdminLevelViewModel(); return View(salvm); } // // POST: /SalesAdminLevel/Create [HttpPost] public ActionResult Add(SalesAdminLevelViewModel viewmodel) { try { SalesAdminLevelModel temp = new SalesAdminLevelModel(); temp.strSalesAdminLevelName = viewmodel.strSalesAdminLevelName; if (SalesAdminLevelBL.isSalesAdminLevelRepeat(temp)) { ModelState.AddModelError("sameSALevelName", Resources.SalesAdminLevel.sameSALevelName); throw new Exception(); } else { viewmodel.blnSalesAdminLevelActive = true; SalesAdminLevelModel temputm = SalesAdminLevelBL.SaveSalesAdminLevel(viewmodel.ToSalesAdminLevelModel()); } return RedirectToAction("Index"); } catch { return View(viewmodel); } } // // GET: /SalesAdminLevel/Edit/5 [HttpPost] public ActionResult Edit1(SALevelListViewModel model) { return RedirectToAction("Edit", new { id = model.SelectID }); } // // GET: /SalesAdminLevel/Edit/5 public ActionResult Edit(int id) { SalesAdminLevelModel utm = new SalesAdminLevelModel(); utm.intSalesAdminLevelID = id; utm = SalesAdminLevelBL.GetSalesAdminLevelInfo(utm); SalesAdminLevelViewModel utViewModel = new SalesAdminLevelViewModel(utm); ViewBag.SALevelID = id; return View(utViewModel); } // // POST: /SalesAdminLevel/Edit/5 [HttpPost] public ActionResult Edit(int id, SalesAdminLevelViewModel utvm) { try { SalesAdminLevelModel temp1 = new SalesAdminLevelModel(); temp1.strSalesAdminLevelName = utvm.strSalesAdminLevelName; temp1.intSalesAdminLevelID = id; ViewBag.SALevelID = id; if (SalesAdminLevelBL.isSalesAdminLevelRepeat(temp1)) { ModelState.AddModelError("sameSALevelName", Resources.SalesAdminLevel.sameSALevelName); temp1 = null; throw new Exception(); } else { utvm.intSalesAdminLevelID = id; SalesAdminLevelModel temputm = SalesAdminLevelBL.UpdateSalesAdminLevel(utvm.ToSalesAdminLevelModel()); } return RedirectToAction("index"); } catch (Exception ex) { SalesAdminLevelModel userTypeModelexp = utvm.ToSalesAdminLevelModel(); return View(new SalesAdminLevelViewModel(userTypeModelexp)); } } // // GET: /SalesAdminLevel/Delete/5 public ActionResult Delete(int id) { SalesAdminLevelModel utm = new SalesAdminLevelModel(); utm.intSalesAdminLevelID = id; //utm = SalesAdminLevelBL.GetSalesAdminLevelByid(utm); if (utm != null) { SalesAdminLevelBL.DeleteSalesAdminLevel(utm); } return RedirectToAction("Index"); } // // POST: /SalesAdminLevel/Delete/5 [HttpPost] public ActionResult Delete(int id, SALevelListViewModel utvm) { try { SalesAdminLevelModel temp = new SalesAdminLevelModel(); temp.intSalesAdminLevelID = utvm.SelectID; if (temp != null) { SalesAdminLevelBL.DeleteSalesAdminLevel(temp); } return RedirectToAction("Index"); } catch { return View(utvm); } } public ActionResult UpdateSalesAdminLevelActive(int id) { SalesAdminLevelViewModel utm = new SalesAdminLevelViewModel(); utm.intSalesAdminLevelID = id; SalesAdminLevelBL.UpdateSalesAdminLevelActive(utm.ToSalesAdminLevelModel()); return RedirectToAction("index"); } } }?