C# 传递参数MVC5的日期选择器
是的,感觉像是一个很难回答的问题,但对这一切来说仍然是新鲜事:) 我在一个库中有一个类,该类应该根据MVC5 web应用程序传递的几个变量生成一个文档 我看了几本教程,但我无法理解,所以我可能是用错误的方法来理解的 这是我的模型:C# 传递参数MVC5的日期选择器,c#,forms,datepicker,asp.net-mvc-5,C#,Forms,Datepicker,Asp.net Mvc 5,是的,感觉像是一个很难回答的问题,但对这一切来说仍然是新鲜事:) 我在一个库中有一个类,该类应该根据MVC5 web应用程序传递的几个变量生成一个文档 我看了几本教程,但我无法理解,所以我可能是用错误的方法来理解的 这是我的模型: public class SummaryTicketsReportModel { public bool ServiceDesk { get; set; } [DisplayFormat(D
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"/>
}
使用System.Drawing
@模型摘要ticketsreportmodel
@{
ViewBag.Title=“TicketsSummaryReport”;
}
票证摘要报告
@使用(Html.BeginForm())
{
@TextBox(“,String.Format(“{0:d}”,Model.StartDate))
@TextBox(“,String.Format(“{0:d}”,Model.EndDate))
@Html.CheckBoxFor(model=>model.ServiceDesk)
}
为了使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();
}
我还没有测试过它,所以希望没有其他问题。你的问题是什么?它有点不起作用:(比如因为“并非所有代码都返回值”而没有运行)看起来您没有从控制器操作返回任何内容,注释掉的方法返回什么?如果它是
SummaryTicketsReportModel
您可能需要类似返回视图(generateReport.MonthSummaryReport());
谢谢!帮了我一点忙,但现在我必须做一些其他事情:)干杯