我正在尝试将2个参数从JQuery的Autocomplete插件传递到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();
}
答案 0 :(得分:2)
通过jquery / ajax发布时,你必须发布为,
'<%= Url.Action("AddSharedUser", "Entity") %>' + 'id=42&name=Russ'
不确定^组成什么,如果它不起作用,请尝试:
'/Entity/AddSharedUser?id=42&name=Russ'
或尝试在UrlAction中指定参数
'<%= Url.Action("AddSharedUser", "Entity", new { id = "42", name = "Russ" }) %>'