将参数传递给mvc中的ajax的Javascript函数

时间:2018-06-14 07:27:06

标签: javascript ajax asp.net-mvc parameters

我有一个我无法处理的问题。我有一个ASP.NET MVC应用程序。它的目的是在里面列出物品。这些项目是项目活动及其财务报告。在视图中,用户假设查看项目活动的财务信息并编辑某些值。以下是观点:

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.10.4-3 readr_1.1.1        

loaded via a namespace (and not attached):
[1] compiler_3.5.0   assertthat_0.2.0 R6_2.2.2         cli_1.0.0       
[5] hms_0.4.2        tools_3.5.0      pillar_1.2.2     rstudioapi_0.7  
[9] tibble_1.4.2     yaml_2.1.19      crayon_1.3.4     Rcpp_0.12.16    
[13] utf8_1.1.3       pkgconfig_2.0.1  rlang_0.2.0

如上所述,javascript函数 readTender 需要 projectActivityId 作为读取投标信息的输入。我将 projectActivityId 写入视图的最后一个td标记。如何将 projectActivityId 传递给 readTender ()?

以下是 ProjectActivityFinancialReportViewModel 的内容:

@model IEnumerable<DKMPPIB.Presentation.ViewModelKlasor.VarlikKlasoru.ProjectActivityFinancialReportViewModel>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Region)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.City)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ProjectName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FieldName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ProjectActivityName)
        </th>           
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>     
            <td>
                @Html.DisplayFor(modelItem => item.Region)
            </td>      
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td> 
            <td>
                @Html.DisplayFor(modelItem => item.ProjectName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FieldName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ProjectActivityName)
            </td>            
            <td>
                <a onclick="readTender()" >
                    <img src='@Url.Content("~/Content/Images/glyphicons_free/glyphicons-342-briefcase.png")' />
                </a>                    
            </td>
            <td>
                <a href='@Url.Action("MyAction", "MyController")'>
                    <img src='@Url.Content("~/Content/Images/glyphicons_free/glyphicons-459-money.png")' />
                </a>
            </td>    
            @*Take attention to that line. I embed the ProjectActivityId here.*@                
            <td style="visibility:hidden" id="satirProjeFaaliyetId">@Html.DisplayFor(modelItem => item.ProjectActivityId)</td> 

        </tr>        
    }

</table>


<div id="tender-message" title="Tender Information" style="visibility:hidden">
    <p>
        <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
        Here is your tender detail: 
    </p>
    <p>
        Currently using <b>36% of your budget </b>.
    </p>
</div>


<script type="text/javascript">


    function readTender(){

    //I will make an ajax call to get tender information. I need ProjectActivityId for this.
        alert("read tender");
        //var projectActivityId = $('#ProjectActivityId').find(":selected").val();
        //console.log(bolgeDropdownId);

        //$.ajax({
        //    type: "POST",
        //    url: '/Erp/GetTenderInformation',
        //    data: "{ProjectActivityId :'" + projectActivityId + "'}",
        //    contentType: "application/json; charset=utf-8",
        //    dataType: "json",
        //    success: successFunc,
        //    error: errorFunc
        //});
    }

</script>

2 个答案:

答案 0 :(得分:0)

<td style="visibility:hidden" id="satirProjeFaaliyetId">@Html.DisplayFor(modelItem => item.ProjectActivityId)</td>

问题在于,您尝试按不存在的ID查找元素:

$('#ProjectActivityId').find(":selected").val();

所以将顶行更改为以下内容:

<td style="visibility:hidden;" id="ProjectActivityId">@item.ProjectActivityId</td>

和js-code中的行

$('#ProjectActivityId').val();

答案 1 :(得分:0)

您可以处理每行的点击事件。 我想“选择”是通过行上的“点击”来处理的。 你可以这样做:

  1. 更改您的javascript功能:
  2. function readTender(projectActivityId) { ... }

    1. 在foreach中更改你的tr:
    2. <tr onclick="javascript:readTender(@item.ProjectActivityId)">; ... <tr>