简体   繁体   English

如何使用Html.ActionLink将文本框(DatePicker)变量传递给控制器​​?

[英]How do I pass a textbox (DatePicker) variable to my controller using Html.ActionLink?

I have a link that opens a pdf in a new window, without leaving the page. 我有一个链接,可在不离开页面的情况下在新窗口中打开pdf。 I had this link working... 我有这个链接工作...

<script type="text/javascript">
$(function () {

    $("#DatePicker").mask("99/99/9999").datepicker({ maxDate: new Date() });
});
if (document.images) {
    var pic1 = new Image(100, 200);
    pic1.src = '<%=Url.Content("~/images/calendarContent.png") %>'
}
</script>

<%= Html.ActionLink("Click ME", "Controller", "Home", new { id = Model.id }, new { onclick = "stayHomeFunc()"})%></div>

After a review, I have to add a DatePicker function that allows the user to select a date. 经过审查,我必须添加一个DatePicker函数,该函数允许用户选择一个日期。 How do I get to pass that date selection to my controller? 如何将日期选择传递给我的控制器? This is what I have so far, which returns a null startDate by the way... 这就是我到目前为止所拥有的,顺便说一句,它返回一个空的startDate ...

Enter Date:<input name="DatePicker" id="DatePicker" type="text" style="width: 80px" />
<%= Html.ActionLink("Click ME", "Controller", "Home", new { id = Model.id, startDate = DatePicker }, new { onclick = "stayHomeFunc()"})%></div>


public ActionResult COntroller(string id, string startDate){...}

Any ideas? 有任何想法吗? Thanks in advance... 提前致谢...

You have 2 possibilities: 您有2种可能性:

  1. use a submit button inside the form containing the #DatePicker field. 在包含#DatePicker字段的表单内使用提交按钮。 This way you don't need to pass anything, when the form is submitted all input values will automatically be sent to the server: 这样,您无需传递任何内容,提交表单后,所有输入值将自动发送到服务器:

     @using (Html.BeginForm("SomeAction", "Home")) { @Html.TextBoxFor(x => x.DatePicker) <input type="submit" value="Click Me" /> } 
  2. if you want to use an anchor you will need to use javascript in order to append the value of the datepicker to the query string. 如果要使用锚,则需要使用javascript才能将datepicker的值附加到查询字符串。 So inside your stayHomeFunc function which is triggered when the link is clicked: 因此,在您的stayHomeFunc函数内部, stayHomeFunc函数在单击链接时触发:

     function stayHomeFunc(link) { var date = $('#DatePicker').datepicker('getDate'); var formattedDate = $.datepicker.formatDate('yy-mm-dd', date); link.href += '?DatePicker=' + formattedDate; } 

    and then don't forget to pass the anchor instance to the onclick event: 然后不要忘记将锚点实例传递给onclick事件:

     <%= Html.ActionLink( "Click ME", "SomeAction", "Home", new { id = Model.id }, new { onclick = "stayHomeFunc(this)"} ) %> 

Personally I would go with the first option as it is semantically more correct and doesn't require any javascript. 就个人而言,我会选择第一个选项,因为它在语义上更正确,并且不需要任何JavaScript。

Also be careful with the DateTime format that the model binder uses and the differences that exist between GET and POST requests. 还应注意模型绑定程序使用的DateTime格式以及GET和POST请求之间存在的差异。 For more information refer to the following article . 有关更多信息,请参阅以下文章

You can also use FormCollection. 您也可以使用FormCollection。

VIEW 视图

 @using (Html.BeginForm("SomeAction", "Home"))
{

<input type="text" id="DatePicker"  name="date">
}

CONTROLLER 控制器

   public ActionResult SomeAction( FormCollection form)
    {
     var date = form["date"];

               if (date != String.Empty)
                {
                    MyModel model = new MyModel();
                    model.date= DateTime.Parse(date);
                }
       return RedirectToAction("YourNewAction", new {date = model.date});

    }

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

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