简体   繁体   English

C# MVC 将日期选择器值传递给 controller

[英]C# MVC Pass datepicker value to controller

I have a view where the user can select a new date.我有一个视图,其中用户可以 select 一个新日期。 The value that is being passed back to the controller is the default value not the new selected one.传递回 controller 的值是默认值,而不是新选择的值。

Controller: 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>
}

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:在你的 js 上使格式相同:

dateFormat: "yyyy-MM-dd" 

also in your action you should convert date同样在你的行动中你应该转换日期

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

I have a view where the user can select a new date.我有一个视图,用户可以在 select 一个新日期。 The value that is being passed back to the controller is the default value not the new selected one.传回 controller 的值是默认值,而不是新选择的值。

Controller: 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>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM