0

I have a view where the user can select a new date. The value that is being passed back to the controller is the default value not the new selected one.

Controller:

public ActionResult ManualLoad(string source, string datepicker1)
{
    List<string> loadMessages = new List<string>();
    try
    {
        Lib.RunDate = datepicker1;
        loadMessages = Lib.AutoLoad(source);
    }
    catch (Exception e)
    {
        string errMsg = e.Message + ((e.InnerException == null) ? "" : e.InnerException.Message);
        ViewBag.ErrorMessage = errMsg;
    }

    return View(loadMessages);
}

View:

@{
    ViewBag.Title = "Loan Records";
    var date1 = String.Format("{0:yyyy-MM-dd}", String.IsNullOrEmpty(ViewBag.date1) ? DateTime.Today : ViewBag.date1);

    string warnMsg = "<div class=\"badge alert-danger btn-lg\">" + ProjectFunctions.getAdministratorWarnings() + "</div>";
}

    <p style="margin: 20px 20px 20px 20px">
        <label for="datepicker1">Enter Specific Run Date:</label>
        <input type="text" id="datepicker1" name="datepicker1" style="width:90px">
    </p>

  <p style="margin: 20px 20px 20px 20px">
      @Html.RouteLink("Select file for Manual LOAD for " + Lib.SOURCE_1, new { controller = "FTP", action = "ManualLoad", source = Lib.SOURCE_1, datepicker1 = date1, autoload = true })
      <span>
          Load and Process a new dataset (loan tape and loan docs) in one selection.
          <br />[ Retrieves loan tape files from: @Html.Raw(ProjectFunctions.getSourceDirectory(Lib.SOURCE_1, true)) ]
          <br />[ Retrieves loan docs from: @Html.Raw(ProjectFunctions.getLoanDocsRetrieveDirectory(Lib.SOURCE_1, true)) ]
          <br />[ Working Copies to: @Html.Raw(ProjectFunctions.getLoanDocsLoadDirectory(Lib.SOURCE_1)) ]
      </span>
  </p>

<p>@Html.RouteLink("Return to Administrative Tasks", new { controller = "Admin", action = "Index" })</p>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryui")

    <script type="text/javascript">
        $().ready(function() {
            $("#datepicker1").datepicker({
                showButtonPanel: true,
                minDate: new Date(2020, 1, 1),
                maxDate: new Date(),
                dateFormat: "yy-mm-dd"
            });

            $("#datepicker1").val('@date1');

        });

    </script>
}

1 Answer 1

1

it is because of date formats are different;

 var date1 = String.Format("{0:yyyy-MM-dd}", String.IsNullOrEmpty(ViewBag.date1) ? DateTime.Today : ViewBag.date1);

on your js make the format same:

dateFormat: "yyyy-MM-dd" 

also in your action you should convert date

Lib.RunDate = DateTime.ParseExact(datepicker1, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.