1

我正在使用 ASP.Net MVC4 和 Jquery UI 日期选择器来处理日期字段我想知道如何使用代码中的变量。

@Html.ActionLink("新建", "创建")

<script>
    $(function () {
        $("#from").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onSelect: function (selectedDate) {
                $("#to").datepicker("option", "minDate", selectedDate);
            }
        });

        $("#to").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onSelect: function (selectedDate) {
                $("#from").datepicker("option", "maxDate", selectedDate);
            }
        });
    });
</script>


<p>
<label for="from">From</label>
<input type="date" id="from" name="from" value='<%=from%>'/>
<label for="to">to</label>
<input type="date" id="to" name="to" value='<%=to%>'/>
</p>

我想使用从“from”/“to”字段中获得的值并在我的 Controller 中使用它们。

  public ActionResult index( )
        {
            DateTime first = 

            return View();
        }
4

2 回答 2

0

从:

<label for="from">From</label>
<input type="date" id="from" name="from" value='<%=from%>'/>
<label for="to">to</label>
<input type="date" id="to" name="to" value='<%=to%>'/>

到:

@using(Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)){
    <label for="from">From</label>
    <input type="date" id="from" name="from" value='<%=from%>'/>
    <label for="to">to</label>
    <input type="date" id="to" name="to" value='<%=to%>'/>
    <input type="submit" value="Submit Form" />
}
于 2013-04-06T16:12:03.007 回答
0

您可以使用 HTML 表单:

@using (Html.BeginForm())
{
    <p>
        <label for="from">From</label>
        <input type="date" id="from" name="from" value='<%=from%>'/>
        <label for="to">to</label>
        <input type="date" id="to" name="to" value='<%=to%>'/>
    </p>    
    <button type="submit">OK</button>
}

和一个控制器动作,它将被调用并作为参数传递给字段:

[HttpPost]
public ActionResult Index(DateTime from, DateTime to)
{
    // when the form is submitted this action will be invoked and passed the 
    // selected values
    ...
}
于 2013-04-06T16:12:57.180 回答