0

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();
}
2
  • How does the POST body look like? I can't see anything wrong with your code. I don't understand from your question what parameters are being sent to the action and what is wrong. Commented Apr 4, 2011 at 14:34
  • Darin, I wasn't sure exactly what you wanted so I've copied the entire page into the post. Commented Apr 4, 2011 at 14:59

1 Answer 1

2

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" }) %>'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.