2014-10-29 34 views
0

右鍵,感覺就像一個很小白的問題,但還是新的這一切:)日期選擇器來傳遞參數MVC5

我在圖書館一類是應該基於幾個變量來生成文檔應該從MVC5網絡應用程序傳遞。

我看了一些教程,但我無法理解它,所以也許我正在接近這個錯誤的方式嗎?

這是我的模型:

 public class SummaryTicketsReportModel 
     { 
      public bool ServiceDesk { get; set; } 

      [DisplayFormat(DataFormatString = "{0:DD/MM/YYYY", ApplyFormatInEditMode = true)] 
      [DataType(DataType.Date)] 
      [DisplayName("From")] 
      public DateTime StartDate { get; set; } 

      [DisplayFormat(DataFormatString = "{0:DD/MM/YYYY", ApplyFormatInEditMode = true)] 
      [DataType(DataType.Date)] 
      [DisplayName("From")] 
      public DateTime EndDate { get; set; } 


    //Do I need this? 
      //public SummaryTicketsReportModel() 
      //{ 
      // StartDate = new DateTime(); 
      // EndDate = new DateTime(); 
      //} 

這是我的控制器:

public class SummaryReportController : Controller 
    { 
     // GET: SummaryReport 
     public ActionResult Index() 
     { 

      return View(); 
     } 

     //POST Action 
     [HttpPost] 
     public ActionResult Index(SummaryTicketsReportModel serviceDesk, SummaryTicketsReportModel startDate, SummaryTicketsReportModel endDate) 
     { 
      // takes in the view model 
      var selectedServiceDesk = serviceDesk; 
      var selectedStartDate = startDate; 
      var selectedEndDate = endDate; 
      var generateReport = new TicketSummaryReport(); 
//Needs to access the following: MonthSummaryReport (ServiceDesk, StartDate, EndDate, summaryDocX) 
      //return generateReport.MonthsSummaryReport(); 
     } 
    } 

這是我的看法:

@using System.Drawing 
@model SummaryTicketsReportModel 

@{ 
    ViewBag.Title = "TicketsSummaryReport"; 
} 


<h2>TicketsSummaryReport</h2> 

@using (Html.BeginForm()) 
{ 
    <tr> 
     <td> 
      @Html.TextBox("", String.Format("{0:d}", Model.StartDate)) 

     </td> 
     <td> 
      @Html.TextBox("", String.Format("{0:d}", Model.EndDate)) 
     </td> 
     <td style="text-align: center"> 
      @Html.CheckBoxFor(model => model.ServiceDesk) 

     </td> 
    </tr> 
    <input type="submit"/> 
} 
+0

你的問題是什麼? – 2014-10-29 11:01:23

+0

它有點拒絕工作:(因爲「不是所有的代碼都返回值」,所以沒有運行) – 2014-10-29 11:04:46

+1

看起來你沒有從你的控制器動作返回任何東西,你註釋掉的方法返回什麼?如果它是'SummaryTicketsReportModel '你很可能想要類似'return View(generateReport.MonthsSummaryReport());' – 2014-10-29 11:07:05

回答

2

爲了MVC模型綁定工作, HTML表單元素的id必須與SummaryTicketsReportModel的屬性名稱匹配。

所以,你需要做的是:

@Html.TextBox("StartDate", String.Format("{0:d}", Model.StartDate)) 
@Html.TextBox("EndDate", String.Format("{0:d}", Model.EndDate)) 

或者,使用您在SummaryTicketsReportModel已經應用了註釋善良:

@Html.TextBoxFor(model => model.StartDate) 
@Html.TextBoxFor(model => model.EndDate) 

在你的控制器,試試這個:

[HttpPost] 
public ActionResult Index(SummaryTicketsReportModel model) 
{ 
    // takes in the view model 
    var selectedServiceDesk = model.ServiceDesk; 
    var selectedStartDate = model.StartDate; 
    var selectedEndDate = model.EndDate; 

    //The rest of your code 

    return View(); 
} 

我沒有測試過這個,所以希望沒有其他錯誤。

+0

謝謝!幫助了我一下,但是現在我必須修復其他一些事情:)乾杯! – 2014-10-30 08:33:42