繁体   English   中英

使用asp.net mvc对话框进行jquery

[英]jquery with asp.net mvc dialog box

我有一个HTML链接,我想弹出一个包含用户名和复选框列表的对话框。 然后,用户可以选择一个或多个复选框,然后单击“确定”,该信息将被传递回主GUI。 使用jquery可以做到这一点还是我应该使用其他技术吗?

没有jQuery是完美的工具。

您所需要做的就是在控制器中发回到ActionResult中,返回数据(甚至更好的局部视图)并将其显示在页面上。

如果您需要示例代码,请告诉我,我会发布一些示例代码。

但实际上,您需要执行$.post("/controller/action", {arg1:val1, arg2:val2}, function(retHtml){ code to show data });

在您的控制器中,像这样;

public ActionResult action(string arg1, string arg2)
{
  //Do guff
  return PartialView("MyPartialView", FormViewModel);
}

如果您需要解释的样本,请在评论中告诉我。

编辑:

我提供的代码实际上是相当完整的,但让我们对其加以充实并使其简单。 有更好的方法可以做到这一点,但是如果您不熟悉jQuery,这将很容易阅读。

让我们从视图开始;

您因此说了一个按钮:

<input id="submitBtn" name="submitBtn" type="submit" onclick="postComment(<%=Model.Id %>); return false;" value="Submit" />

然后,您就可以像这样发布jQuery代码了;

function postComment(id) {
        var commentText = jQuery.trim($("#textbox_ + id.toString()).val());
        $.post("/Articles/jQueryAddComment", { commentText: commentText, id: id, }, function(newCommentListHTML) {
            AddCommentReturn(id, commentType, newCommentListHTML);
        });
    }

上面的代码所做的只是从一个字段中获取注释文本,然后将其回发到我的jQueryAddComment控制器动作中,并传入一些变量。

现在在我的控制器中;

public ActionResult jQueryAddComment(string commentText, int id)
{
  //code here to add the new comment to the database.

  //more code to get the new list of comments from the database and into a model

  //code to return a partial view back to the view itself
  return PartialView("CommentList", fvm);

}

上面的jQuery代码中的回调函数调用普通的Javascript函数来获取返回的HTML并将其显示在页面上。

在这种情况下,您将显示其中包含HTML的Div并向其提供click事件,以便用户可以与其进行交互。

这更清楚吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM