【问题标题】:Newbie binding MVC forms jquery新手绑定MVC表单jquery
【发布时间】:2012-12-28 16:04:32
【问题描述】:

我可以通过直接发布到不同的控制器/操作来做到这一点。 Say I had a list of orders and when select an order I get the order in edit mode, how would I do this with jquery(i.e. no post backs etc all on the one page) and post the edit and return to the list.我仍想使用数据注释等进行验证。干杯。

【问题讨论】:

    标签: jquery model-view-controller


    【解决方案1】:

    我假设您使用的是 Spring MVC。我通过使用$.ajax() 函数并使用jQuery Form Plugin 序列化Spring 绑定表单来实现这一点。请参阅下面的示例:

    围绕您的 Spring MVC 表单创建一个容器 <div id="container"></div>

        $.ajax({
            type: 'POST',
            url: actionUrl,
            data: $('#id_of_your_form_tag').serialize(),
            success: function(data) {
                $('#container').html(data);
            },
            async: false
        });
    

    【讨论】:

    • 对,这是有道理的。渲染表单怎么样。我正在考虑使用 ajax 渲染局部视图,例如这个链接。这是推荐的做法吗?
    • 对,这是有道理的。渲染表单怎么样。我正在研究像这样使用 ajax 渲染局部视图。这是推荐的方法吗。

      $.get( '@Url.Action("details","user", new { id = Model.ID } )', function(data) { $('#detailsDiv').replaceWith(数据); });一件事是我如何传递ID。回到我原来的例子。我有一个订单列表,上面的代码显示了编辑表单。上面的jquery怎么知道model.id来使用上面的例子呢?见stackoverflow.com/questions/1570127/…
    • 出于好奇,我在 Ipad 上执行此操作,如何换行。返回只是发表评论:)
    • 提供的示例是呈现表单。 actionUrl 应该从表单的“action”属性中获得。您可以将模型 ID 作为隐藏字段保留在表单中。
    • 我认为我的方法是正确的。我将为每个订单生成带有 ajax 操作链接的订单列表,其中订单 ID 在路由参数中传递。将其指向订单详细信息的部分视图并呈现它并使用原始建议使用 ajax 发布表单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2010-09-23
    • 2018-07-23
    • 2012-05-03
    • 2012-07-25
    • 1970-01-01
    • 2011-06-30
    相关资源
    最近更新 更多