简体   繁体   English

调用javascript函数并传递参数

[英]calling javascript function and passing parameter

I'm using a Kendo Grid and I'm trying to create a custom column template command to call a JavaScript function that does an ajax call to my mvc controller passing two parameters so that I can do some other stuff in there. 我正在使用Kendo Grid,并且试图创建一个自定义列模板命令来调用JavaScript函数,该JavaScript函数对传递两个参数的mvc控制器进行ajax调用,以便在那里执行其他操作。

This is the grid: 这是网格:

@(Html.Kendo().Grid<iPlan.Syspro.Beekman.Portal.Agents.Models.SalesOrderDetailViewModel>()
.Name("Details")
.HtmlAttributes(new {@style = "width:80vw"})
.Columns(columns => {
    columns.Bound(c => c.Agent).Width(100);
    columns.Bound(c => c.SalesOrder).Width(150);
    columns.Bound(c => c.Line).Width(60);
    columns.Bound(c => c.StockCode).Width(150);
    columns.Bound(c => c.SerialNumber).Width(150);
    columns.Bound(c => c.DerivativeDescription).Width(150);
    columns.Bound(c => c.StockCodeDescription).Width(150);
    columns.Bound(c => c.OrderQty).Width(150);
    columns.Bound(c => c.OnBackorder).Width(150);
    columns.Bound(c => c.QtyAvailable).Width(150);
    columns.Bound(c => c.ShippedQuantity).Width(150);
    columns.Bound(c => c.SalesPrice).Width(150);
    columns.Bound(c => c.AlternativeSerial).Width(150);
    columns.Bound(c => c.AlternativeSerialReason).Width(150);
    columns.Bound(c => c.VinNr).Width(150);
    columns.Bound(c => c.DealerOrderNr).Width(150);
    columns.Bound(c => c.WipNr).Width(150); 
    columns.Bound(c => c.GrnNr).Width(150);
    columns.Bound(c => c.AsnNr).Width(150);
    columns.Bound(c => c.DeliveryNoteNr).Width(150);  
    columns.Command(command => {command.Edit(); command.Destroy();}).Width(172);
    columns.Template(@<text></text>).Width(150).ClientTemplate("<a class='k-button k-button-       icontext k-grid-edit' href='javascript:' onclick='approve()'><span class='k-icon k-edit'>  </span>Approve</a>").Title("Action");      
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Sortable()
.Scrollable()
.Groupable()
.Pageable(pageable => pageable
    .Refresh(true)
    .PageSizes(true)
    .ButtonCount(5))
.DataSource(dataSource => dataSource
    .Ajax()
    .Events(events => events.Error("error_handler"))
    .Read(read => read.Action("SalesOrdersDetail_Read", "Inbox").Data("getSalesOrder"))
    .Update(update => update.Action("SalesOrdersDetail_Update","InboxAgent"))
    .Destroy(destroy => destroy.Action("SalesOrdersDetail_Destroy","InboxAgent"))
    .Create(create => create.Action("SalesOrdersDetail_Create","InboxAgent"))
    .Model(model => {
        model.Id(p => p.SalesOrder);
        model.Field(p => p.StockCode).Editable(false);
        model.Field(p => p.SerialNumber).Editable(false);
        model.Field(p => p.DerivativeDescription).Editable(false);
        model.Field(p => p.StockCodeDescription).Editable(false);
        model.Field(p => p.SalesOrder).Editable(false);            
        model.Field(p => p.Agent).Editable(false);
        model.Field(p => p.Line).Editable(false);
        model.Field(p => p.OrderQty).Editable(false);
        model.Field(p => p.OnBackorder).Editable(false);
        model.Field(p => p.QtyAvailable).Editable(false);
        model.Field(p => p.ShippedQuantity).Editable(false);
    })        
))

Here is the JavaScript function I'm trying to call: 这是我要调用的JavaScript函数:

  <script type="text/javascript">
  function approve(e) {
    debugger;        
    console.log("index function hit");
    //var data = $("#Details").data("kendoGrid").dataSource.data();
    //var dataRet = data.indexOf(dataItem);
    $.ajax({
        type: 'POST',
        url: href="/Inbox/SalesOrderDetailApprove" + "?SalesOrder=" + e.SalesOrder + "&Line=" + e.Line,
        dataType: 'json',
        data: {} ,
        success: function (response) { }
    });
}

Now, at the moment with this code, it hits the JavaScript method, but it doesn't pass through my data. 现在,使用此代码,它可以使用JavaScript方法,但不会传递我的数据。 This is the method in the controller it needs to pass the data to. 这是控制器中需要将数据传递到的方法。 So the problems that needs to be solved is this: 所以需要解决的问题是这样的:

  1. The data needs to be passed correctly to the JavaScript method. 数据需要正确地传递给JavaScript方法。
  2. As soon as I click on my button in the template column, the kendo grid goes into edit mode and I need to click the button again to get it out of edit mode. 单击模板列中的按钮后,kendo网格便进入编辑模式,我需要再次单击该按钮才能使其脱离编辑模式。
  3. The event isn't firing once, it hits the method when the page is loaded. 该事件不会触发一次,它会在页面加载时命中该方法。
  4. Another issue is that I installed resharper recently (trial). 另一个问题是我最近安装了resharper(试用版)。 I uninstalled resharper and from there on out my intellisense on all the razor parts in the views (cshtml) is broken. 我卸载了resharper,从那以后,视图(cshtml)中所有剃须刀部件的智能感知都坏了。 How do I fix this? 我该如何解决?
public ActionResult SalesOrderDetailApprove( string  SalesOrder, string Line) {
    try {
        return Json("", JsonRequestBehavior.AllowGet);
    } catch (Exception) {
        throw;
    }
}

Try this code to call the function 试试下面的代码来调用函数

function approve(e) {
  var postUrl = '@Url.Action("SalesOrderDetailApprove", "Inbox")';
  var EmployeeId = { SalesOrder: e.SalesOrder, Line:  e.Line};
    $.ajax({
            url: postUrl,
             data: EmployeeId,
             type: 'POST',
             dataType: 'json',
             success: function (response) { }
      });
 }

This will work for you. 这将为您工作。

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

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