Javascript 使用dropdownlist中的参数从视图调用Web API
今天,我有一个这样的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
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实现这一点的方法是