简体   繁体   English

将数据从视图发送到控制器

[英]Sending data from view to the controller

Inside my view I have two jquery date input fields with two buttons for processing those date selections. 在我的视图内部,我有两个带有两个按钮的jquery日期输入字段,用于处理这些日期选择。 On the first button I'm send data over ajax and display data inside some div which works perfect. 在第一个按钮上,我通过ajax发送数据并在某个div内显示数据,这很完美。

Beside that button I have one more button which displays data inside pdf document. 在该按钮旁边,我还有一个按钮,可在pdf文档中显示数据。 Ofcourse I do not want to use ajax for that but I do want to use same selected date data (I do now want to use two more inputs for handling user selection). 当然,我不想为此使用ajax,但我确实希望使用相同的选定日期数据(我现在想再使用两个输入来处理用户选择)。

so my view code snippet is something like this 所以我的视图代码片段是这样的

<tr>
    <td>@Html.TextBox("dateFrom", null, new { @class = "date", @id = "datePickerFrom" })</td>
    <td>@Html.TextBox("dateTo", null, new { @class = "date", @id = "datePickerTo" })</td>
    <td>
        <input type="submit" value="Send" class="dateBtn" /></td>
     <td>
        <input type="submit" value="PDF" class="dateBtnPdf" />
     </td>

</tr>

and I'm having js which handles click handler and send data to the controller for ajax manipulation 而且我有处理点击处理程序并将数据发送到控制器以进行Ajax操作的js

$(document).ready(function () {
   $(".dateBtn").click(function (event) {
      var dateFrom = $("#datePickerFrom").val();
      var dateTo = $("#datePickerTo").val();
      GetDetails(dateFrom, dateTo);
   })
function GetDetails(dateFrom, dateTo) {
    $.ajax({
..... ommited on purpose

so my question is how can I send this selected values to the controller for the second button which will not use ajax (do I need to use form and if so how to use this same input fields to grab value which will be sent to the controller?) 所以我的问题是我如何将选择的值发送到第二个不使用ajax的按钮的控制器(我是否需要使用表格,如果要使用相同的输入字段来获取将发送到控制器的值?)

 $(".dateBtnPdf").click(function (event) {
   var dateFrom = $("#datePickerFrom").val();
   var dateTo = $("#datePickerTo").val();

   //send values to the controller /report/pdfDemo
 })

Thanks 谢谢

I see few solutions to this situation, i suggest the one you are close to: 对于这种情况,我看不到有什么解决方案,我建议您选择一种接近的解决方案:

<form id="form-dates" action"/report/pdfDemo"> <!-- Here you could use @Url.Action("pdfDemo","report") -->

@Html.Hidden("dateFromPdf");
@Html.Hidden("dateToPdf");

<input type="submit" value="PDF" class="dateBtnPdf" />

</form>

And in your javascript, update the hidden values of the form and submit it: 然后在您的JavaScript中,更新表单的隐藏值并提交:

$(".dateBtnPdf").click(function (event) {
   $('[name="dateFromPdf"]').val( $("#datePickerFrom").val() );
   $('[name="dateToPdf"]').val( $("#datePickerTo").val() );

   $('#form-dates').submit();
   //send values to the controller /report/pdfDemo
 })

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

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