从 JQuery Autocomplete 插件将参数传递给 ASP.Net MVC2 Action

发布于 2024-10-30 01:07:49 字数 1257 浏览 1 评论 0原文

我使用以下代码将 2 个硬编码参数传递给 ASP.Net MVC2 控制器操作:

<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", new { id = "42", snlid="17394" }) %>',
                function (data) { })
            }
        });
    });
</script>

这工作正常,但现在我需要更改 $.post 中传入的值以来自我的模型,所以我在想一些事情就像下面的代码一样,但这不起作用。关于如何解决这个问题有什么想法吗?

<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", new { id = '<%= Model.EntityId  %>', name= '<%= Model.Name  %>' }) %>',
                function (data) { })
            }
        });
    });
</script>

I'm passing 2 hardcoded parameters to an ASP.Net MVC2 Controller Action with this code:

<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", new { id = "42", snlid="17394" }) %>',
                function (data) { })
            }
        });
    });
</script>

This works fine, but now I need to change the values passed in in the $.post to come from my Model, so I'm thinking something like the following code, but that doesn't work. Any ideas on how to fix this?

<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", new { id = '<%= Model.EntityId  %>', name= '<%= Model.Name  %>' }) %>',
                function (data) { })
            }
        });
    });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

网白 2024-11-06 01:07:49

这个怎么样?

<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", new { id = Model.EntityId, name = Model.Name }) %>',
                function (data) { })
            }
        });
    });
</script>

不过,如果是我,我会提取该数据,使其更具语义:

<input id="term" data-source-url="<%= Url.Action("GetEntitySharedUsers", "Search") %>" data-select-url="<%= Url.Action("AddSharedUser", "Entity", new { id = Model.EntityId, name = Model.Name }) %>" />

<script type="text/javascript">
    $(document).ready(function () {
        var term = $("form#search_for_entity_user input#term");

        term.autocomplete({
            source: term.data('source-url'),
            delay: 200,
            minLength: 3,
            select: function (event, ui) {
                $.post(term.data('select-url'), function (data) { });
            }
        });
    });
</script>

How about this?

<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", new { id = Model.EntityId, name = Model.Name }) %>',
                function (data) { })
            }
        });
    });
</script>

If it were me, though, I would extract that data so it is more semantic:

<input id="term" data-source-url="<%= Url.Action("GetEntitySharedUsers", "Search") %>" data-select-url="<%= Url.Action("AddSharedUser", "Entity", new { id = Model.EntityId, name = Model.Name }) %>" />

<script type="text/javascript">
    $(document).ready(function () {
        var term = $("form#search_for_entity_user input#term");

        term.autocomplete({
            source: term.data('source-url'),
            delay: 200,
            minLength: 3,
            select: function (event, ui) {
                $.post(term.data('select-url'), function (data) { });
            }
        });
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文