简体   繁体   English

如何在MVC 5中将日期选择器应用于文本框而不绑定到模型日期字段

[英]How to apply datepicker to a textbox in mvc 5 without binding to a model date field

Please how can we apply a date-picker to a text-box that is not bound to any model field? 请如何将日期选择器应用于未绑定到任何模型字段的文本框?

For example, I have a 2 text boxes, dt1 & dt2 used to pass a date range to a controller, but the model for the this controller has as no date fields. 例如,我有2个文本框,dt1和dt2用于将日期范围传递给控制器​​,但是此控制器的模型没有日期字段。

My view: 我的观点:

<script type="text/javascript">
    $(function () {
        // This will make every element with the class "date-picker" into a DatePicker element
        $('.date-picker').datepicker();
    })
</script>

<div>
    <h2>Settled Report</h2><br />
    @using (Html.BeginForm("SettledReports", "POSReportController", FormMethod.Get))
    {
        <p>
            Accounts: @Html.DropDownList("accountNumber")
            Start Date: @Html.TextBox("dt1")
            End Date: @Html.TextBox("dt2")

            <input type="submit" value="View Report" />
        </p>
    }
    <h3>Transaction Breakdown</h3><br />
    <table class="table">
        <tr><th>SN</th><th>Card Type</th><th>Trans Count</th><th>Total Gross</th><th>Total Surcharge</th><th>Total Net</th></tr>
        @foreach (var item in Model.summary)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.sn)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.cardType)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.transCount)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.gross)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.surcharge)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.net)
                </td>
            </tr>
        }
    </table>
    <br />
    <table class="table">
        <tr><td>SN</td><td>Merchant</td><td>Trans Date</td><td>Merchant ID</td><td>Terminal ID</td><td>STAN</td><td>Card No.</td><td>Trans Type</td><td>Approval Code</td><td>Gross Amount</td><td>POS Charge</td><td>Net Amount</td><td>Settlement Date</td></tr>
        @foreach (var item in Model.detail)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.sn)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.merchant)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.transDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.merchantID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.terminalID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.stan)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.cardNo)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.transType)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.approvalCode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.gross)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.posCharge)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.net)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.settlementDate)
                </td>
            </tr>
        }
    </table>
</div>

Use 采用

@Html.TextBox("dt1", null, new {@class="date-picker"})

and move 并移动

<script type="text/javascript">
    $(function () {
        // This will make every element with the class "date-picker" into a DatePicker element
        $('.date-picker').datepicker();
    })
 </script>

after the </div> </div>

If you're using bootstrap-datepicker (you can install it with this NuGet package ), you can create a daterange with 如果您正在使用bootstrap-datepicker (可以使用此NuGet软件包安装它),则可以使用以下命令创建日期范围

<div class="input-daterange">
    @Html.TextBox("dt1", null, new {@class="input-small"})
    <span class="add-on">to</span>
    @Html.TextBox("dt2", null, new {@class="input-small"})
</div>

and the javascript code, 和JavaScript代码,

$(".input-daterange input").each(function (){
    $(this).datepicker(“clearDates”);
});

I face the same problem and that is my steps to solve it: 我面临同样的问题,这是我要解决的步骤:
1. install bootstrap datepicker: 1.安装引导日期选择器:
right click on solution --> manage nuget packages for solution --> search for bootstrap datepicker and install it 右键单击解决方案->管理解决方案的nuget软件包->搜索引导日期选择器并安装它
2. in file _Layout.cshtml add those files "bootstrap-datepicker.css & bootstrap-datepicker.js" : 2.在_Layout.cshtml文件中添加以下文件“ bootstrap-datepicker.css和bootstrap-datepicker.js”:
Note: add the files after bootstrap and jquery script files to be like that : 注意:在bootstrap和jquery脚本文件之后添加文件,如下所示:

<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/bootstrap.js"></script>

@*Define Bootstrap DatePicker to use in index page*@
<link href="~/Content/bootstrap-datepicker.css" rel="stylesheet" />
<script src="~/Scripts/bootstrap-datepicker.js"></script>
  1. in your view page add the html element for the datepicker: 在您的视图页面中,为datepicker添加html元素:

    @Html.TextBox("datepicker1", null, new { @class = "date-picker" }) @ Html.TextBox(“ datepicker1”,null,新的{@class =“ date-picker”})

  2. use this script: 使用此脚本:

  <script type="text/javascript"> $(function () { // This will make every element with the class "date-picker" into a DatePicker element $('.date-picker').datepicker(); }) </script> 

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

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