日期选择器传递参数MVC5

时间:2014-10-29 10:57:30

标签: c# forms datepicker asp.net-mvc-5

对,感觉就像一个非常的菜鸟问题,但对所有这些仍然是新的:)

我在一个库中有一个类,它应该根据应该从MVC5 Web应用程序传递的一些变量生成一个doc。

我查看了几个教程,但我无法理解它,所以也许我接近这个错误的方式?

这是我的模特:

      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"/>
}

1 个答案:

答案 0 :(得分: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();
}

我没有测试过这个,所以希望没有其他错误。

相关问题