0

在 MVC 4 中将参数从视图传递到控制器时,我遇到了一个令人困惑的问题。问题是我创建了一个视图并且创建了以下代码:

 <% using (Html.BeginForm("DisplaySummaryFigures", "Report", FormMethod.Post, new { Par1 = "some", Par2 = "some" }))
  { %>

    <%: Html.ValidationSummary(true)%>

    <table id="tblInsertDates">
        <tr>
            <th>
                <asp:Label ID="Label1" runat="server" Text="lblStratDate"> Insert the Start Date:</asp:Label>
                <input type="date" name="TxtStartDate" value="" />
            </th>
            <th>
                <asp:Label ID="Label2" runat="server" Text="lblEndDate" Style="padding-left: 5px">Insert the End Date:</asp:Label>
                <input type="date" name="TxtEndDate" value="" /><br />
            </th>
        </tr>
        <tr>
            <td>
                <input type="submit" name="SubmitDates" value="Enter" /><br />

            </td>

        </tr>
    </table>

在我的控制器内部,我将它们传递如下:

[HttpGet]
    public ActionResult ExportSummaryFiguresToCSV()

    [HttpPost]
    public ActionResult ExportSummaryFiguresToCSV(DateTime TxtStartDate, DateTime TxtEndDate)
    {


        StringWriter sw = new StringWriter();
        DateTime dtStartDateFromUser = TxtStartDate;
        DateTime dtEndDateFromUser = TxtEndDate;

当我运行程序时返回以下错误:(:

The parameters dictionary contains a null entry for parameter 'TxtStartDate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult TotalSharepointDetails(System.DateTime, System.DateTime)' in 'SalesStaticsWebSite.Controllers.ReportController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

参数名称:parametersere

请问有什么想法吗??提前致谢

4

3 回答 3

2

我会建议你创建一个模型,并使用强类型视图

public class MyModel
{
    [Required]  
    public DateTime StartDate {get; set;}

    [Required]  
    public DateTime EndDate {get; set;}
}

然后通过这个

[HttpGet]
public ActionResult ExportSummaryFiguresToCSV()
{
    return View(new MyModel());
}

看法:

@model MyModel
<% using (Html.BeginForm("DisplaySummaryFigures", "Report", FormMethod.Post, new { Par1 = "some", Par2 = "some" }))
{ %>
    <% Html.TextBoxFor( m => m.StartDate)%>
    <% Html.TextBoxFor( m => m.EndDate)%>

控制器

[HttpPost]
public ActionResult ExportSummaryFiguresToCSV(MyModel model)
{       
    DateTime dtStartDateFromUser = model.StartDate;
    DateTime dtEndDateFromUser = model.EndDate;
于 2013-08-09T12:05:50.173 回答
1

该框架正在尝试填充输入参数的值,但是它遇到了日期为空值的问题(可能是因为客户端没有为它们提交任何值,例如尚未设置字段)。您可以通过确保字段中输入了一个值(如默认值或如果其中任何一个适合您的业务模型,则将它们设为必需)或使输入类型可为空(DateTime?)而不是 DateTime 来解决此问题。

于 2013-08-09T12:05:43.580 回答
1

发布的参数可能会从调用中省略。然后,活页夹将无法提出任何有意义的值(默认值除外)。

这就是为什么您必须通过使其可以用null值调用它来使其明确。

[HttpPost]
public ActionResult ExportSummaryFiguresToCSV(
   DateTime? TxtStartDateParam, 
   DateTime? TxtEndDateParam)
{
    // first, get values  
    DateTime TxtStartDate = 
      TxtStartDateParam != null ? TxtStartDateParam.Value : __put a default value here__;
    DateTime TxtEndDate   = 
      TxtEndDateParam != null ? TxtEndDateParam.Value : __put a default value here__;

    // this follows unchanged
    StringWriter sw = new StringWriter();
    DateTime dtStartDateFromUser = TxtStartDate;
    DateTime dtEndDateFromUser   = TxtEndDate;
于 2013-08-09T12:06:06.050 回答