0

I have a confusing issue with passing parameters from a view to controller in MVC 4. The problem is that I have created a view and I have created the following code :

 <% 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>

inside my controller, I have passed them as the following:

[HttpGet]
    public ActionResult ExportSummaryFiguresToCSV()

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


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

when I run the program is returns the following Error :( :

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.

Parameter name: parametersere

Any ideas please ?? Thanks in Advance

3 Answers 3

2

I will suggest you to create a model like, and use strongly-type view

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

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

And pass this when

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

View:

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

Controller

[HttpPost]
public ActionResult ExportSummaryFiguresToCSV(MyModel model)
{       
    DateTime dtStartDateFromUser = model.StartDate;
    DateTime dtEndDateFromUser = model.EndDate;
Sign up to request clarification or add additional context in comments.

1 Comment

Problem when use this way is sometime at client , they filling difference format datatime. so when you got value on controller. will be error
1

Posted parameters could possibly be omited from the call. The binder would then not be able to come up with any meaningful values (except for default values).

This is why you have to make it explicit by making it possible to call this with null values.

[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;

2 Comments

Thanks for your help , but what do you mean by put a default value here? can you gives example please ?
An example would be new DateTime(2000, 1, 1) or DateTime.Now
1

The framework is trying to populate the values for the input parameters however it is running into the problem of null value for the dates (probably because the client did not submit any values for them, e.g the fields have not been set). You can solve this either by making sure the fields have a value entered in them (like a default value or making them required if either of these fit with your business model) or making the input type nullable (DateTime?) instead of DateTime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.