从 JQuery 自动完成将参数传递给 ASP.Net MVC2 操作
我试图将 2 个参数从 JQuery 的自动完成插件传递到 ASP.Net MVC2 Action,请参阅下面的脚本。 Controller 名为 EntityController,Action 名为 AddSharedUser,它采用 2 个字符串,请参阅下面也复制的 Action。当我尝试运行它时,它尝试将“AddSharedUser”作为单个参数传递,但失败了。有什么想法我哪里出错了吗?
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<UI.Models.FormModel.EntitySharedUserContainer>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit Entity Shared Users
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Entity - <%= Model.EntityName%>, Edit Shared Users</h2>
<ul class="entity-shared-users">
<% foreach (var item in Model.SharedUsersList)
{ %>
<li>
<%: item.Name%>
<%: Html.ActionLink("Remove", "RemoveSharedUser", "Entity",
new { id = Model.EntityId, CustId = item.CustId }, null)%>
</li>
<% } %>
</ul>
<form id="search_for_entity_user" method="get" action="<%= Url.Action("AddSharedUser", "Entity") %>">
<label for="term">Add Shared User:</label>
<%= Html.TextBox("term")%>
</form>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Notify" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageTitle" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="JavaScriptContent" runat="server">
<script type="text/javascript">
$(document).ready(function () {
$("form#search_for_entity_user input#term").autocomplete({
source: '<%= Url.Action("GetEntitySharedUsers", "Search") %>',
delay: 200,
minLength: 3,
select: function (event, ui) {
$.post('<%= Url.Action("AddSharedUser", "Entity") %>',
{ id: '42', name: 'Russ' },
function (data) { alert("x"); })
}
});
});
</script>
</asp:Content>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddSharedUser(string id, string name)
{
//Trying to use the parameters here
return View();
}
I'm trying to pass 2 parameters from JQuery's Autocomplete plugin to an ASP.Net MVC2 Action, see script below. The Controller is named EntityController, and the Action is named AddSharedUser, which takes 2 strings, see Action also copied below. When I try to run it it tries to pass in "AddSharedUser" as a single parameter, and fails. Any ideas where I've gone wrong?
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<UI.Models.FormModel.EntitySharedUserContainer>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit Entity Shared Users
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Entity - <%= Model.EntityName%>, Edit Shared Users</h2>
<ul class="entity-shared-users">
<% foreach (var item in Model.SharedUsersList)
{ %>
<li>
<%: item.Name%>
<%: Html.ActionLink("Remove", "RemoveSharedUser", "Entity",
new { id = Model.EntityId, CustId = item.CustId }, null)%>
</li>
<% } %>
</ul>
<form id="search_for_entity_user" method="get" action="<%= Url.Action("AddSharedUser", "Entity") %>">
<label for="term">Add Shared User:</label>
<%= Html.TextBox("term")%>
</form>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Notify" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageTitle" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="JavaScriptContent" runat="server">
<script type="text/javascript">
$(document).ready(function () {
$("form#search_for_entity_user input#term").autocomplete({
source: '<%= Url.Action("GetEntitySharedUsers", "Search") %>',
delay: 200,
minLength: 3,
select: function (event, ui) {
$.post('<%= Url.Action("AddSharedUser", "Entity") %>',
{ id: '42', name: 'Russ' },
function (data) { alert("x"); })
}
});
});
</script>
</asp:Content>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddSharedUser(string id, string name)
{
//Trying to use the parameters here
return View();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过 jquery/ajax 发布时,您必须发布为,
不完全确定 ^ 的组成,因此如果它不起作用,请尝试:
或尝试在 UrlAction 中指定参数
When posting trough jquery/ajax you have to post as,
Not exactly sure what ^ makes up so if it doesnt work try:
or try specifying the parameters in the UrlAction