在mvc4中将参数从视图传递到控制器

时间:2013-08-09 11:58:47

标签: asp.net-mvc-4

我在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

有什么想法吗? 在此先感谢

3 个答案:

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

答案 1 :(得分:1)

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

答案 2 :(得分: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;
相关问题