mirror of
https://github.com/rudollee/LearningMVC.git
synced 2026-08-11 16:32:55 +00:00
Ch. 23 URL and Ajax Helper Methods
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using HelperMethods.Models;
|
||||
|
||||
namespace HelperMethods.Controllers
|
||||
{
|
||||
public class PeopleController : Controller
|
||||
{
|
||||
private Person[] personData =
|
||||
{
|
||||
new Person {FirstName = "Adam", LastName = "Freeman", Role = Role.Admin},
|
||||
new Person {FirstName = "Jacqui", LastName = "Friffyth", Role = Role.User},
|
||||
new Person {FirstName = "John", LastName = "Smith", Role = Role.User},
|
||||
new Person {FirstName = "Anne", LastName = "Jones", Role = Role.Guest}
|
||||
};
|
||||
|
||||
public ActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
//public ActionResult GetPeople()
|
||||
//{
|
||||
// return View(personData);
|
||||
//}
|
||||
|
||||
//[HttpPost]
|
||||
//public ActionResult GetPeople(string selectedRole)
|
||||
//{
|
||||
// if (selectedRole == null || selectedRole == "All")
|
||||
// {
|
||||
// return View(personData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Role selected = (Role)Enum.Parse(typeof(Role), selectedRole);
|
||||
// return View(personData.Where(p => p.Role == selected));
|
||||
// }
|
||||
//}
|
||||
|
||||
//private IEnumerable<Person> GetData(string selectedRole)
|
||||
//{
|
||||
// IEnumerable<Person> data = personData;
|
||||
// if (selectedRole != "All")
|
||||
// {
|
||||
// Role selected = (Role)Enum.Parse(typeof(Role), selectedRole);
|
||||
// data = personData.Where(p => p.Role == selected);
|
||||
// }
|
||||
// return data;
|
||||
//}
|
||||
|
||||
//public JsonResult GetPeopleDataJson(string selectedRole = "All")
|
||||
//{
|
||||
// var data = GetData(selectedRole).Select(p => new
|
||||
// {
|
||||
// FirstName = p.FirstName,
|
||||
// LastName = p.LastName,
|
||||
// Role = Enum.GetName(typeof(Role), p.Role)
|
||||
// });
|
||||
|
||||
// //IEnumerable<Person> data = GetData(selectedRole);
|
||||
// return Json(data, JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
|
||||
//public PartialViewResult GetPeopleData(string selectedRole = "All")
|
||||
//{
|
||||
// return PartialView(GetData(selectedRole));
|
||||
//}
|
||||
|
||||
public ActionResult GetPeopleData(string selectedRole = "All")
|
||||
{
|
||||
IEnumerable<Person> data = personData;
|
||||
if (selectedRole != "All")
|
||||
{
|
||||
Role selected = (Role)Enum.Parse(typeof(Role), selectedRole);
|
||||
data = personData.Where(p => p.Role == selected);
|
||||
}
|
||||
|
||||
if (Request.IsAjaxRequest())
|
||||
{
|
||||
var formattedData = data.Select(p => new
|
||||
{
|
||||
FirstName = p.FirstName,
|
||||
LastName = p.LastName,
|
||||
Role = Enum.GetName(typeof(Role), p.Role)
|
||||
});
|
||||
return Json(formattedData, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
return PartialView(data);
|
||||
}
|
||||
}
|
||||
|
||||
public ActionResult GetPeople(string selectedRole = "All")
|
||||
{
|
||||
return View((object)selectedRole);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user