从 JQuery 自动完成将参数传递给 ASP.Net MVC2 操作

发布于 2024-10-30 05:20:24 字数 2269 浏览 0 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平安喜乐 2024-11-06 05:20:24

通过 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" }) %>'

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" }) %>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文