关于使用jQuery将多个参数传递给ASP.NETWebAPI的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于asp.net–AJAX将多个参数传递给WebApi、asp.net–如何将r
关于使用jQuery将多个参数传递给ASP.NET Web API的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于asp.net – AJAX将多个参数传递给WebApi、asp.net – 如何将razor变量作为参数传递给jquery函数、asp.net-mvc – mvc将多个参数传递给局部视图、asp.net-mvc – 使用jQuery.post将多个参数发布到MVC Controller等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- 使用jQuery将多个参数传递给ASP.NET Web API
- asp.net – AJAX将多个参数传递给WebApi
- asp.net – 如何将razor变量作为参数传递给jquery函数
- asp.net-mvc – mvc将多个参数传递给局部视图
- asp.net-mvc – 使用jQuery.post将多个参数发布到MVC Controller
使用jQuery将多个参数传递给ASP.NET Web API
$.ajax({ url: 'api/products',type: 'POST',data: { Id: 2012,Name: 'test',Category: 'My Category',Price: 99.5 },dataType: 'json',success: function (data) { alert(data); } });
但它不工作怎么做?
解决方法
$.ajax({ url: 'api/products',data: JSON.stringify({ Id: 2012,Price: 99.5 }),success: function (data) { alert(data); } });
asp.net – AJAX将多个参数传递给WebApi
$.ajax({ url: url,dataType: 'json',type: 'Post',data: {token:"4",Feed:{"id":0,"message":"Hello World","userId":4} } });
服务器端Web API:
[HttpPost] public HttpResponseMessage Post(string token,Feed Feed) { /* Some code */ return new HttpResponseMessage(HttpStatusCode.Created); }
Error Code 404: {“message”:”No HTTP resource was found that matches
the request URI ‘localhost:8080/api/Feed’.”,”messageDetail”:”No action
was found on the controller ‘Feed’ that matches the request.”}
为什么我收到此错误以及为什么我无法将多个参数POST到我的API?
解决方法
public class Myviewmodel { public string Token { get; set; } public Feed Feed { get; set; } }
您的控制器操作将作为参数:
[HttpPost] public HttpResponseMessage Post(Myviewmodel model) { /* Some code */ return new HttpResponseMessage(HttpStatusCode.Created); }
最后调整你的jQuery调用将其作为JSON发送:
$.ajax({ url: url,type: 'POST',contentType: 'application/json',data: JSON.stringify({ token: '4',Feed: { id: 0,message: 'Hello World',userId: 4 } }) });
AJAX调用需要注意的重要事项:
>将请求contentType设置为application / json>将数据包装在JSON.stringify函数中,以有效地将javascript对象转换为JSON字符串>删除无用的dataType:’json’参数. jQuery将自动使用服务器发送的Content-Type响应头来推断如何解析传递给成功回调的结果.
asp.net – 如何将razor变量作为参数传递给jquery函数
< a href =“#”onclick =“编辑(@Interest);”>编辑< / a>
我在哪里
@ {string Interest =“”}
解决方法
<a href="#" onclick="Edit('@Interest');">edit</a>
asp.net-mvc – mvc将多个参数传递给局部视图
<% Html.RenderPartial("Details",Model.test,new ViewDataDictionary { { "labelName","Values1" },{"header","Header1"},{"header2","Header2"}}); %>
代码,我有错误信息
) missing.
怎么了?
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MvcUI.Models.Label>>" %> <%var label = ViewData["labelName"];%> <%int count = 0; %> <%if (Model!=null) {%> <% foreach (var model in Model){ %> <%if (!String.IsNullOrEmpty(model.Name)) {%> <li> <%: Html.Hidden((label)+".Index",count.ToString())%> <%: Html.TextBox((label)+"[" + (count) + "].Name",model.Name,new {})%> <%: Html.Hidden((label)+"[" + (count++) + "].ID",model.ID,new {})%> <input type="button" value = "Delete"/> </li> <%} %> <%} %> <% } %>
解决方法
public class MyModel { public string Test { get; set; } public string LabelName { get; set; } public string Header { get; set; } public string Header2 { get; set; } }
然后简单地说:
<% Html.RenderPartial("Details",Model); %>
除此之外,你的语法没有任何问题.你得到的错误来自其他地方:
<% Html.RenderPartial( "Details",new ViewDataDictionary { { "labelName",{ "header","Header1" },{ "header2","Header2" } } ); %>
更新:
现在您已经展示了代码,让我建议您使用编辑器模板更清晰的方法.
首先定义一个模型:
public class MyModel { public IEnumerable<Label> Labels { get; set; } } public class Label { public string ID { get; set; } public string Name { get; set; } }
然后一个控制器将填充此模型:
public class HomeController : Controller { public ActionResult Index() { var model = new MyModel { Labels = new[] { new Label { ID = "l1",Name = "Label 1" },new Label { ID = "l2",Name = "Label 2" },new Label { ID = "l3",Name = "Label 3" } } }; return View(model); } }
然后是视图(〜/ Views / Home / Index.aspx):
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SomeNs.Models.MyModel>" %> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <ul> <%: Html.EditorFor(x => x.Labels) %> </ul> </asp:Content>
最后是编辑器模板(〜/ Views / Home / EditorTemplates / Label.ascx):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Models.Label>" %> <li> <%: Html.HiddenFor(x => x.ID) %> <%: Html.TextBoxFor(x => x.Name) %> <input type="button" value="Delete" /> </li>
如您所见,使用编辑器模板,您不再需要担心命名输入,维护和增加索引,编写循环,所有这些容易出错的事情都由框架自动处理.
asp.net-mvc – 使用jQuery.post将多个参数发布到MVC Controller
[AcceptVerbs(HttpVerbs.Post)] public JsonResult PostMoreData(DataContracts.Address address,DataContracts.GeoLocation geoLocation) { return Json("test"); } where DataContracts.Address and DataContracts.GeoLocation are complex types.
从我的视图我试图使用jQuery发布如下:
function PostMoreData() { var JsonAddress = { "Building": $('Building').val(),"UnitNumber": $('UnitNumber').val(),"StreetNumber": $('StreetNumber').val(),"StreetName": $('StreetName').val(),"StreetType": $('StreetType').val(),"Suburb": $('Suburb').val(),"State": $('State').val(),"Postcode": $('Postcode').val(),"MonthsAtAddress": $('MonthsAtAddress').val() }; var JsonGeoLocation = { "Latitude": $('Latitude').val(),"Longitude": $('Longitude').val() }; jQuery.post("/AddressValidation/PostMoreData",{address: JsonAddress,geoLocation: JsonGeoLocation},function(data,textStatus) { if (textStatus == "success") { var result = eval(data); if (result.length > 0) { alert(result); } } },"json"); }
但是,在控制器上,我得到空值.
它可以工作,如果我的控制器只需要一个参数,我只发布一个对象.
[AcceptVerbs(HttpVerbs.Post)] public JsonResult PostMoreData(DataContracts.Address address) { return Json("test"); }
function PostMoreData() { var JsonAddress = { "Building": $('Building').val(),"MonthsAtAddress": $('MonthsAtAddress').val() }; jQuery.post("/AddressValidation/PostMoreData",JsonAddress,"json"); }
任何想法如何我可以发布多个对象?
解决方法
address=[object]&geoLocation=[object]
另一个工作示例不包含任何子对象,因此它是直接翻译的,如下所示:
Building=value&UnitNumber=value&...&MonthsAtAddress=value
最简单的解决方法是使参数映射平坦,每个键都以“地址”为前缀.或’GeoLocation.’,取决于.
我们今天的关于使用jQuery将多个参数传递给ASP.NET Web API的分享已经告一段落,感谢您的关注,如果您想了解更多关于asp.net – AJAX将多个参数传递给WebApi、asp.net – 如何将razor变量作为参数传递给jquery函数、asp.net-mvc – mvc将多个参数传递给局部视图、asp.net-mvc – 使用jQuery.post将多个参数发布到MVC Controller的相关信息,请在本站查询。
本文标签: