Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用dropdownlist中的参数从视图调用Web API_Javascript_C#_Jquery_Asp.net Mvc - Fatal编程技术网

Javascript 使用dropdownlist中的参数从视图调用Web API

Javascript 使用dropdownlist中的参数从视图调用Web API,javascript,c#,jquery,asp.net-mvc,Javascript,C#,Jquery,Asp.net Mvc,今天,我有一个这样的Web Api: public HttpResponseMessage Get() { NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query, Encoding.UTF8); string Report = nvc["Report"].ToString(); Int64 Id = Convert.ToInt64(nvc["Id"]); va

今天,我有一个这样的Web Api:

public HttpResponseMessage Get()
{
    NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query, Encoding.UTF8);
    string Report = nvc["Report"].ToString();
    Int64 Id = Convert.ToInt64(nvc["Id"]);
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    switch (Report)
    {
        case "PackLista":
            response.Content = new StreamContent(this.PackLista(Id));
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            break;
    }
    return response;
}
@using (@Html.BeginForm("Index", "Meels", FormMethod.Get))
{
    <div class="form-horizontal">
        <div class="form-group">
            <div class="col-md-3">                    
                <label class="control-label ">Choose:</label>
                 @Html.DropDownList("SearchString", (IList<SelectListItem>)ViewBag.Htos, "-- All --", new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                <input  value="Report" class="btn btn-primary" onclick="cmdPrint_onclick()"/>
            </div>
        </div>

    </div>
}

<script>
    function cmdPrint_onclick() 
    {
            // 
            window.location.href = '/api/Report/Report?report=PackLista&Id=2' ;
    }

</script>
对于测试,我使用javascript通过按钮和硬编码参数调用此API,如下所示:

public HttpResponseMessage Get()
{
    NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query, Encoding.UTF8);
    string Report = nvc["Report"].ToString();
    Int64 Id = Convert.ToInt64(nvc["Id"]);
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    switch (Report)
    {
        case "PackLista":
            response.Content = new StreamContent(this.PackLista(Id));
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            break;
    }
    return response;
}
@using (@Html.BeginForm("Index", "Meels", FormMethod.Get))
{
    <div class="form-horizontal">
        <div class="form-group">
            <div class="col-md-3">                    
                <label class="control-label ">Choose:</label>
                 @Html.DropDownList("SearchString", (IList<SelectListItem>)ViewBag.Htos, "-- All --", new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                <input  value="Report" class="btn btn-primary" onclick="cmdPrint_onclick()"/>
            </div>
        </div>

    </div>
}

<script>
    function cmdPrint_onclick() 
    {
            // 
            window.location.href = '/api/Report/Report?report=PackLista&Id=2' ;
    }

</script>
@使用(@Html.BeginForm(“Index”,“Meels”,FormMethod.Get))
{
选择:
@DropDownList(“SearchString”,(IList)ViewBag.Htos,“--All--”,新的{@class=“form control”})
}
函数cmdPrint\u onclick()
{
// 
window.location.href='/api/Report/Report?Report=PackLista&Id=2';
}
现在我想使用下拉列表中的值作为javascript中的参数
Id
。或者jQuery是正确的方法?我该怎么做? 正如您看到的,API返回的是pdf格式的流,因此结果将打开一个pdf文档

  • JS版本是

  • jQuery实现这一点的方法是

  • 区别仅在于我们用于获取下拉列表的选定值的脚本