2013-02-22 355 views
0

我正在建立在線預訂系統,以便人們可以在線預約醫生預約。我粗略地構建了我的MVC 4控制器,以便我可以使用存儲過程對其進行測試,我必須爲每位醫生提供可用的約會。MVC 4控制器 - 使用動態的StaffID's和BusinessID而不是硬編碼的

下面你會看到我已經在控制器中硬編碼了10名醫生,但我需要從URL中提取醫生ID &醫生的數量可能會有所不同(第一頁可能有10位醫生,但只有1位醫生醫生在最後一頁)。

注意:我還需要將「BusinessID」添加到存儲過程的控制器&,因爲每位醫生都可以在多個業務中工作。

的URL看起來像(DOC ID的將有2個部分:STAFFID是-BusinessID):

http://mydomain.com/Bookings?start=2013-01-01&StaffID=1-1&StaffID=2-1&StaffID=3-1&StaffID=4-1&StaffID=5-1&StaffID=6-1 

我的問題是:

  1. 我怎樣才能改變控制器,以便醫生ID's(StaffID's)& BusinessID可以通過url動態完成(請參閱上面的示例URL)?
  2. 如何在URL中爲每個SaffID-BusinessID動態構建控制器中的部件(請參閱註釋中的*部分)?
  3. 我如何更好地優化這個?

----獲取可用時隙控制器----

 public ActionResult Index(DateTime? start) 
    { 
     if (!start.HasValue) 
     { 
      start = DateTime.Today; 
     } 

     //get date information (this will always be 7 days) 
     var day1 = start.Value.Date; 
     var day2 = day1.AddDays(1); 
     var day3 = day1.AddDays(2); 
     var day4 = day1.AddDays(3); 
     var day5 = day1.AddDays(4); 
     var day6 = day1.AddDays(5); 
     var day7 = day1.AddDays(6); 

     //***NOTE: THIS IS HARD CODED TO TEN DOCTORS BUT WILL NEED TO BE ABLE TO GET A DYNAMIC AMOUNT OF DOCTORS AS SOME PAGES MAY ONLY HAVE A FEW DOCTORS ON IT 
     //NOTE: Stored Procedure = SP_GetAvailableAppointments @StaffID int = 0, @StartDate Date = NULL, @NumberOfDays INT = 1); 
     //get doc1 appointments for the next 7 days 
     var model1 = db.SP_GetAvailableAppointments(1, start, 1); 
     var model2 = db.SP_GetAvailableAppointments(1, day2, 1); 
     var model3 = db.SP_GetAvailableAppointments(1, day3, 1); 
     var model4 = db.SP_GetAvailableAppointments(1, day4, 1); 
     var model5 = db.SP_GetAvailableAppointments(1, day5, 1); 
     var model6 = db.SP_GetAvailableAppointments(1, day6, 1); 
     var model7 = db.SP_GetAvailableAppointments(1, day7, 1); 

     //get doc2 appointments for the next 7 days 
     var model8 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model9 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model10 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model11 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model12 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model13 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model14 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //get doc3 appointments for the next 7 days 
     var model15 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model16 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model17 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model18 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model19 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model20 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model21 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //get doc4 appointments for the next 7 days 
     var model22 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model23 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model24 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model25 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model26 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model27 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model28 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //get doc5 appointments for the next 7 days 
     var model29 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model30 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model31 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model32 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model33 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model34 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model35 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //get doc6 appointments for the next 7 days 
     var model36 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model37 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model38 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model39 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model40 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model41 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model42 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //get doc7 appointments for the next 7 days 
     var model43 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model44 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model45 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model46 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model47 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model48 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model49 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //get doc8 appointments for the next 7 days 
     var model50 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model51 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model52 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model53 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model54 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model55 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model56 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //get doc9 appointments for the next 7 days 
     var model57 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model58 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model59 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model60 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model61 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model62 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model63 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //get doc10 appointments for the next 7 days 
     var model64 = db.SP_GetAvailableAppointments(2, day1, 1); 
     var model65 = db.SP_GetAvailableAppointments(2, day2, 1); 
     var model66 = db.SP_GetAvailableAppointments(2, day3, 1); 
     var model67 = db.SP_GetAvailableAppointments(2, day4, 1); 
     var model68 = db.SP_GetAvailableAppointments(2, day5, 1); 
     var model69 = db.SP_GetAvailableAppointments(2, day6, 1); 
     var model70 = db.SP_GetAvailableAppointments(2, day7, 1); 

     //new Json Object - myDates 
     var myDates = new 
     { 
      date1 = day1.ToShortDateString(), 
      date2 = day2.ToShortDateString(), 
      date3 = day3.ToShortDateString(), 
      date4 = day4.ToShortDateString(), 
      date5 = day5.ToShortDateString(), 
      date6 = day6.ToShortDateString(), 
      date7 = day7.ToShortDateString(), 
      dname1 = day1.DayOfWeek.ToString(), 
      dname2 = day2.DayOfWeek.ToString(), 
      dname3 = day3.DayOfWeek.ToString(), 
      dname4 = day4.DayOfWeek.ToString(), 
      dname5 = day5.DayOfWeek.ToString(), 
      dname6 = day6.DayOfWeek.ToString(), 
      dname7 = day7.DayOfWeek.ToString(), 
      ndate = day1.AddDays(7).ToString("yyyy-MM-dd"), //next start date 
      pdate = day1.AddDays(-7).ToString("yyyy-MM-dd") //previous start date 
     }; 

     //***NOTE: THIS IS HARD CODED TO TEN DOCTORS BUT WILL NEED TO BE ABLE TO GET A DYNAMIC AMOUNT OF DOCTORS AS SOME PAGES MAY ONLY HAVE A FEW DOCTORS ON IT 
     //new Json Object - doc1 
     var doc1 = new 
     { 
      staffid = 1, 
      day1 = model1, 
      day2 = model2, 
      day3 = model3, 
      day4 = model4, 
      day5 = model5, 
      day6 = model6, 
      day7 = model7 
     }; 

     //new Json Object - doc2 
     var doc2 = new 
     { 
      staffid = 2, 
      day1 = model8, 
      day2 = model9, 
      day3 = model10, 
      day4 = model11, 
      day5 = model12, 
      day6 = model13, 
      day7 = model14 
     }; 

     //new Json Object - doc3 
     var doc3 = new 
     { 
      staffid = 3, 
      day1 = model15, 
      day2 = model16, 
      day3 = model17, 
      day4 = model18, 
      day5 = model19, 
      day6 = model20, 
      day7 = model21 
     }; 

     //new Json Object - doc5 
     var doc4 = new 
     { 
      staffid = 4, 
      day1 = model22, 
      day2 = model23, 
      day3 = model24, 
      day4 = model25, 
      day5 = model26, 
      day6 = model27, 
      day7 = model28 
     }; 

     //new Json Object - doc5 
     var doc5 = new 
     { 
      staffid = 5, 
      day1 = model29, 
      day2 = model30, 
      day3 = model31, 
      day4 = model32, 
      day5 = model33, 
      day6 = model34, 
      day7 = model35 
     }; 

     //new Json Object - doc6 
     var doc6 = new 
     { 
      staffid = 6, 
      day1 = model36, 
      day2 = model37, 
      day3 = model38, 
      day4 = model39, 
      day5 = model40, 
      day6 = model41, 
      day7 = model42 
     }; 

     //new Json Object - doc7 
     var doc7 = new 
     { 
      staffid = 7, 
      day1 = model43, 
      day2 = model44, 
      day3 = model45, 
      day4 = model46, 
      day5 = model47, 
      day6 = model48, 
      day7 = model49 
     }; 

     //new Json Object - doc8 
     var doc8 = new 
     { 
      staffid = 8, 
      day1 = model50, 
      day2 = model51, 
      day3 = model52, 
      day4 = model53, 
      day5 = model54, 
      day6 = model55, 
      day7 = model56 
     }; 

     //new Json Object - doc10 
     var doc9 = new 
     { 
      staffid = 9, 
      day1 = model57, 
      day2 = model58, 
      day3 = model59, 
      day4 = model60, 
      day5 = model61, 
      day6 = model62, 
      day7 = model63 
     }; 

     //new Json Object - doc10 
     var doc10 = new 
     { 
      staffid = 10, 
      day1 = model64, 
      day2 = model65, 
      day3 = model66, 
      day4 = model67, 
      day5 = model68, 
      day6 = model69, 
      day7 = model70 
     }; 

     //Output the Json results 
     return Json(new 
     { 
      myDates, 
      doc1, 
      doc2, 
      doc3, 
      doc4, 
      doc5, 
      doc6, 
      doc7, 
      doc8, 
      doc9, 
      doc10 
     }, JsonRequestBehavior.AllowGet); 

---- JSON結果所需----

{ 
"myDates": { 
    "date1": "22/02/2013", 
    "date2": "23/02/2013", 
    "date3": "24/02/2013", 
    "date4": "25/02/2013", 
    "date5": "26/02/2013", 
    "date6": "27/02/2013", 
    "date7": "28/02/2013", 
    "dname1": "Friday", 
    "dname2": "Saturday", 
    "dname3": "Sunday", 
    "dname4": "Monday", 
    "dname5": "Tuesday", 
    "dname6": "Wednesday", 
    "dname7": "Thursday", 
    "ndate": "2013-03-01", 
    "pdate": "2013-02-15" 
}, 
"doc1": { 
    "staffid": 1, 
    "day1": [{ 
     "ID": 34022, 
     "StaffID": 1, 
     "BusinessID": 1, 
     "SlotDay": "Friday", 
     "SlotTime": { 
      "Ticks": 324000000000, 
      "Days": 0, 
      "Hours": 9, 
      "Milliseconds": 0, 
      "Minutes": 0, 
      "Seconds": 0, 
      "TotalDays": 0.375, 
      "TotalHours": 9, 
      "TotalMilliseconds": 32400000, 
      "TotalMinutes": 540, 
      "TotalSeconds": 32400 
     }, 
     "StartDate": "\/Date(1325336400000)\/", 
     "EndDate": "\/Date(1577797200000)\/", 
     "SlotType": 1, 
     "Created": "\/Date(1361389440000)\/", 
     "CreatedBy": null, 
     "Modified": "\/Date(1361389440000)\/", 
     "ModifiedBy": null, 
     "Active": true, 
     "SlotDate": "\/Date(1361451600000)\/" 
    }] 
} 
} 

---更新: ---- 我能夠從我的控制器獲得以下json輸出,但我需要從myDates & myStaff刪除「key」&「值」,並按日期對約會時間進行分組 - 例如:date0,date1,date2 ..

{ 
"myDates": [{ 
    "Key": "date0", 
    "Value": "23/02/2013" 
}, { 
    "Key": "date1", 
    "Value": "24/02/2013" 
}, { 
    "Key": "date2", 
    "Value": "25/02/2013" 
}, { 
    "Key": "date3", 
    "Value": "26/02/2013" 
}, { 
    "Key": "date4", 
    "Value": "27/02/2013" 
}, { 
    "Key": "date5", 
    "Value": "28/02/2013" 
}, { 
    "Key": "date6", 
    "Value": "1/03/2013" 
}, { 
    "Key": "dname0", 
    "Value": "Saturday" 
}, { 
    "Key": "dname1", 
    "Value": "Sunday" 
}, { 
    "Key": "dname2", 
    "Value": "Monday" 
}, { 
    "Key": "dname3", 
    "Value": "Tuesday" 
}, { 
    "Key": "dname4", 
    "Value": "Wednesday" 
}, { 
    "Key": "dname5", 
    "Value": "Thursday" 
}, { 
    "Key": "dname6", 
    "Value": "Friday" 
}, { 
    "Key": "ndate", 
    "Value": "2013-03-02" 
}, { 
    "Key": "pdate", 
    "Value": "2013-02-16" 
}], 
"myStaff": [{ 
    "Key": "staff0", 
    "Value": [ 
     [{ 
      "SlotID": 42501, 
      "StaffID": 1, 
      "BusinessID": 1, 
      "SlotDate": "23/02/2013", 
      "SlotDay": "Saturday", 
      "SlotTime": "10:00" 
     }, { 
      "SlotID": 42502, 
      "StaffID": 1, 
      "BusinessID": 1, 
      "SlotDate": "23/02/2013", 
      "SlotDay": "Saturday", 
      "SlotTime": "10:30" 
     }], 
     [{ 
      "SlotID": 47001, 
      "StaffID": 1, 
      "BusinessID": 1, 
      "SlotDate": "24/02/2013", 
      "SlotDay": "Sunday", 
      "SlotTime": "10:00" 
     }, { 
      "SlotID": 47002, 
      "StaffID": 1, 
      "BusinessID": 1, 
      "SlotDate": "24/02/2013", 
      "SlotDay": "Sunday", 
      "SlotTime": "10:30" 
     }] 
    ] 
}] 
} 

基本上,我需要的JSON格式如下圖所示:

{ 
"myDates": [{ 
    "date0": "23/02/2013", 
    "date1": "24/02/2013", 
    "date2": "25/02/2013", 
    "date3": "26/02/2013", 
    "date4": "27/02/2013", 
    "date5": "28/02/2013", 
    "date6": "1/03/2013", 
    "dname0": "Saturday", 
    "dname1": "Sunday", 
    "dname2": "Monday", 
    "dname3": "Tuesday", 
    "dname4": "Wednesday", 
    "dname5": "Thursday", 
    "dname6": "Friday", 
    "ndate": "2013-03-02", 
    "pdate": "2013-02-16", 
}], 
"myStaff": [{ 
    "staff0": {[ 
     "date0": {[ 
     [{ 
      "SlotID": 42501, 
      "StaffID": 1, 
      "BusinessID": 1, 
      "SlotDate": "23/02/2013", 
      "SlotDay": "Saturday", 
      "SlotTime": "10:00" 
     }, { 
      "SlotID": 42502, 
      "StaffID": 1, 
      "BusinessID": 1, 
      "SlotDate": "23/02/2013", 
      "SlotDay": "Saturday", 
      "SlotTime": "10:30" 
     }], 
     "date1": {[ 
      "SlotID": 47001, 
      "StaffID": 1, 
      "BusinessID": 1, 
      "SlotDate": "24/02/2013", 
      "SlotDay": "Sunday", 
      "SlotTime": "10:00" 
     }, { 
      "SlotID": 47002, 
      "StaffID": 1, 
      "BusinessID": 1, 
      "SlotDate": "24/02/2013", 
      "SlotDay": "Sunday", 
      "SlotTime": "10:30" 
     }] 
    ]} 
}] 
}] 
} 

這是我的控制器:

 public ActionResult Index(DateTime start, string id = null) 
    { 

     var allids = Request.QueryString["id"]; 

     // split the input into anonymous objects containing staffid and businessid 
     var staffids = from staffid in allids.Split(',').Select(x => x.Split('-')) 
      select new { sid = int.Parse(staffid[0]), bid = int.Parse(staffid[1]) }; 

     // get the days you need 
     var days = Enumerable.Range(0, 7).Select(x => start.AddDays(x)); 

     // create myDates 
     int i = 0; 
     var myDates = (from day in days 
         select new KeyValuePair<string, string>(
          String.Format("date{0}", i++), 
          day.ToShortDateString())).ToList(); 
     i = 0; 
     myDates.AddRange(
         (from day in days 
         select new KeyValuePair<string, string>(
          String.Format("dname{0}", i++), 
          day.DayOfWeek.ToString())).ToList()); 
     myDates.Add(new KeyValuePair<string, string>("ndate", days.First().AddDays(7).ToString("yyyy-MM-dd"))); 
     myDates.Add(new KeyValuePair<string, string>("pdate", days.First().AddDays(-7).ToString("yyyy-MM-dd"))); 

     // receive all the stored_procedures 
     i = 0; 
     var myStaff = from staff in staffids 
         select new KeyValuePair<string, object>(
         String.Format("staff{0}", i++), 
         (from day in days 
          select db.Database.SqlQuery<GetAvailableAppointments>("EXEC SP_GetAvailableAppointments @StaffID, @BusinessID, @StartDate", 
          new SqlParameter("StaffID", staff.sid), 
          new SqlParameter("BusinessID", staff.bid), 
          new SqlParameter("StartDate", day))).ToList() 
        ); 

     return Json(new { myDates, myStaff }, JsonRequestBehavior.AllowGet); 

    } 

任何幫助,將不勝感激:)

回答

1

也許你也想用MVC路由對於這一點,添加新的路由(有ScheduleController)

//Added UrlParameter.Optional so you can set Default values 
routes.MapRoute(
    name: "Schedule", 
    url: "Schedule/Get/{start}/{id}", 
    defaults: new { 
        controller = "Schedule", 
        action = "index", 
        start = UrlParameter.Optional, 
        id = UrlParameter.Optional 
       } 
); 

這樣,你可以只調用操作方法與URL像

http://mydomain.com/Bookings/Get/2013-2-23/1-3,2-5 

如果控制器 - 操作索引具有簽名

public ActionResult Index(DateTime? start, string id = "") 

然後MVC模型綁定將綁定從url中的params自動到簽名變量。

創建你的模型文件夾「MyJsonModels.cs」模型,並用生命填充...

public class Mydate 
{ 
    public string ndate { get; set; } 
    public MyDateNDay[] dates { get; set; } 
    public string pdate { get; set; } 
} 

public class MyDateNDay { 
    public string date {get; set;} 
    public string day { get; set; } 
} 

public class Mystaff 
{ 
    public Staff[] staff { get; set; } 
} 

public class Staff 
{ 
    public int StaffID { get; set; } 
    public int BusinessID { get; set; } 
    public SlotDate[] SlotDates {get; set; } 
} 

public class SlotDate 
{ 
    public string Date { get; set; } 
    public string SlotDay { get; set; } 
    public TimeSlot[] slots { get; set; } 
} 

public class TimeSlot 
{ 
    public int SlotID { get; set; } 
    public string SlotTime { get; set; } 
} 

public class InputData 
{ 
    public int SlotID { get; set; } 
    public int StaffID { get; set; } 
    public int BusinessID { get; set; } 
    public DateTime SlotDate { get; set; } 
    public string SlotDay { get; set; } 
    public string SlotTime { get; set; } 

    public InputData(int sid, int stid, int bid, DateTime day, string time) 
    { 
     SlotID = sid; 
     StaffID = stid; 
     BusinessID = bid; 
     SlotDate = day; 
     SlotDay = day.DayOfWeek.ToString(); 
     SlotTime = time; 
    } 
} 

那麼指數方法中,你將不得不實行真正的行動......(這將不是創建你要求的確切結果,我只是展示瞭如何使用linq來簡化你的代碼並消除冗餘,也許你會用自己創建的對象來存儲結果,而不是使用KeyValuePairs。一個快速,但工作的例子)

public ActionResult Index(DateTime? start, string id = "") 
    { 

     var startdate = start ?? DateTime.Today; 

     // split the input into anonymous objects containing staffid and businessid 
     var staffids = from staffid in id.Split(',').Select(x => x.Split('-')) 
         select new { sid = int.Parse(staffid[0]), bid = int.Parse(staffid[1]) }; 

     // get the days you need 
     var days = Enumerable.Range(0, 7).Select(x => startdate.AddDays(x)); 

     // create myDates 
     int i = 0; 
     Mydate MyDates = new Mydate() { 
      ndate = days.First().AddDays(7).ToString("yyyy-MM-dd"), 
      pdate = days.First().AddDays(-7).ToString("yyyy-MM-dd"), 
      dates = (
         from day in days 
          select new MyDateNDay { 
           date = day.ToShortDateString(), 
           day = day.DayOfWeek.ToString() 
          } 
        ).ToArray() 
     }; 

     // example InputData - Array as it might be returned by the sql procedure 
     /* I added this Array to simulate a possible procedure result your DB could return, 
      maybe you can even add a new procedure that would return the data just as in the 
      following Array 
     */ 
     var input = new [] { 
      new InputData(40501, 1, 1, new DateTime(2013, 02, 20), "09:00"), 
      new InputData(40502, 1, 2, new DateTime(2013, 02, 20), "11:00"), 
      new InputData(42501, 1, 3, new DateTime(2013, 02, 23), "10:00"), 
      new InputData(42502, 1, 3, new DateTime(2013, 02, 23), "10:30"), 
      new InputData(45001, 2, 3, new DateTime(2013, 02, 21), "13:00"), 
      new InputData(45002, 2, 4, new DateTime(2013, 02, 22), "15:30"), 
      new InputData(47001, 2, 5, new DateTime(2013, 02, 24), "10:00"), 
      new InputData(47002, 2, 5, new DateTime(2013, 02, 24), "10:30"), 
     }; 

     // receive all the stored_procedures 
     i = 0; 
     Mystaff MyStaff = new Mystaff() 
     { 
      staff = (from staff in staffids 
        select new Staff() 
        { 
         StaffID = staff.sid, 
         BusinessID = staff.bid, 
         SlotDates = (from day in days 
             select new SlotDate() 
             { 
              Date = day.ToShortDateString(), 
              SlotDay = day.DayOfWeek.ToString(), 
              slots = (from result in input 
                where // filter Slots that fit the requirements 
                 day == result.SlotDate 
                 && result.StaffID == staff.sid 
                 && result.BusinessID == staff.bid 
                select new TimeSlot() 
                { 
                 SlotID = result.SlotID, 
                 SlotTime = result.SlotTime 
                } 
               ).ToArray() 
             } 
            // filter out days that don't have free slots 
            ).Where(x => x.slots.Length > 0).ToArray() 
        } 
        ).ToArray() 
     }; 


     return Json(new { MyDates, MyStaff}, JsonRequestBehavior.AllowGet); 

    } 
} 

T他的操作 - 方法將返回以下JSON

{ 
    "MyDates": 
    { 
     "ndate": "2013-03-02", 
     "dates": [ 
      { 
       "date": "23.02.2013", "day": "Saturday" 
      }, { 
       "date": "24.02.2013", "day": "Sunday" 
      }, { 
       "date": "25.02.2013", "day": "Monday" 
      }, { 
       "date": "26.02.2013", "day": "Tuesday" 
      }, { 
       "date": "27.02.2013", "day": "Wednesday" 
      }, { 
       "date": "28.02.2013", "day": "Thursday" 
      }, { 
       "date": "01.03.2013", "day": "Friday" 
      }], 
     "pdate": "2013-02-16" 
    }, 
    "MyStaff": 
     { 
      "staff": [ 
       { 
        "StaffID": 1, 
        "BusinessID": 3, 
        "SlotDates": [ 
         { 
          "Date": "23.02.2013", 
          "SlotDay": "Saturday", 
          "slots": [ 
           { "SlotID": 42501, "SlotTime": "10:00" }, 
           { "SlotID": 42502, "SlotTime": "10:30" } 
          ] 
         }] 
       }, { 
        "StaffID": 2, 
        "BusinessID": 5, 
        "SlotDates": [ 
         { 
          "Date": "24.02.2013", 
          "SlotDay": "Sunday", 
          "slots": [ 
           { "SlotID": 47001, "SlotTime": "10:00" }, 
           { "SlotID": 47002, "SlotTime": "10:30" } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
}; 

我同意,這不正是你問什麼......但看看它,它實際上是你想要的...它只是我冒昧地刪除冗餘 在JavaScript中,您可以輕鬆瀏覽數據以顯示它。像上面的MyJsonClass一樣使用助手模型可以幫助您在設計Json時以很大的方式進行輸出。

嗯,我希望我沒有忘記任何東西。

+0

感謝您的建議,因爲我可能會實施這之前,我們去住。我認爲我的網址應該實際上http://mydomain.com/Bookings?start=2013-01-01&StaffID=1-1&StaffID=2-1&StaffID= 3-1&StaffID = 4-1&StaffID = 5-1&StaffID = 6-1 – MWD 2013-02-22 07:33:34

+0

我已經更新我的問題與正確的網址..感謝挑選那個ingo :) – MWD 2013-02-22 07:36:07

+0

剛剛發現了一個stackoverflow-問題覆蓋http:// stackoverflow.com/questions/198606/can-i-use-commas-in-a-url – Ingo 2013-02-22 07:46:45

0

第一,請將您的控制器簽名更改爲以下內容:

public ActionResult Index(DateTime? start, string staffID) 

然後,添加一些邏輯來解析staffID到你想要的結果,這樣的事情:

var ids = staffID.Split(','); 
var staffIds = ids.Select(p => p.Split('-').First()).ToList(); 
var businessIds = ids.Select(p => p.Split('-').Last()).ToList(); 

後,您可以根據您的循環上staffIdsbusinessIds列表上方做。

+0

謝謝andri ..這回答了我原來的問題,但我沒有完全解釋我應該有的方式,因爲我也需要知道如何動態構建我的控制器的部分..請參閱我的更詳細的問題。 – MWD 2013-02-22 07:25:44

相關問題