开发者

Passing Parameters to an ASP.Net MVC2 Action from JQuery Autocomplete

I'm trying to pass 2 parameters from JQuery's Autocomplete plugin to an ASP.Net MVC2 Action, see script below. The Controller is name开发者_如何转开发d 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();
}


When posting trough jquery/ajax you have to post as,

'<%= Url.Action("AddSharedUser", "Entity") %>' + 'id=42&name=Russ'

Not exactly sure what ^ makes up so if it doesnt work try:

 '/Entity/AddSharedUser?id=42&name=Russ'

or try specifying the parameters in the UrlAction

'<%= Url.Action("AddSharedUser", "Entity", new { id = "42", name = "Russ" }) %>'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜