简体   繁体   English

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

[英]jquery with asp.net mvc dialog box

i have a html link that i want to popup a dialog box with a list of user names and checkboxes. 我有一个HTML链接,我想弹出一个包含用户名和复选框列表的对话框。 the user can then select one or more checkboxes and click OK and that information will be passed back to the main GUI. 然后,用户可以选择一个或多个复选框,然后单击“确定”,该信息将被传递回主GUI。 is this possible using jquery or should i be using another technology? 使用jquery可以做到这一点还是我应该使用其他技术吗?

No jQuery is the perfect tool for this. 没有jQuery是完美的工具。

All you need to do is post back to your ActionResult in your controller, return your data [or even better partial view] and display it on the page. 您所需要做的就是在控制器中发回到ActionResult中,返回数据(甚至更好的局部视图)并将其显示在页面上。

If you want example code then let me know and I'll post some. 如果您需要示例代码,请告诉我,我会发布一些示例代码。

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

In your controller something like this; 在您的控制器中,像这样;

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

If you need the sample explained then let me know in a comment. 如果您需要解释的样本,请在评论中告诉我。

EDIT: 编辑:

The code I gave is actually reasonably complete but let's flesh it out somewhat and make it simple. 我提供的代码实际上是相当完整的,但让我们对其加以充实并使其简单。 There are better ways of doing this but this is easy and readable if you are new to jQuery. 有更好的方法可以做到这一点,但是如果您不熟悉jQuery,这将很容易阅读。

Let's start with the View; 让我们从视图开始;

You have say a button thus: 您因此说了一个按钮:

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

Then you have the jQuery code to post back like so; 然后,您就可以像这样发布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);
        });
    }

What the code above is doing is simply grabbing the comment text from say a field and posting back to my controller action of jQueryAddComment and passing in a few variables. 上面的代码所做的只是从一个字段中获取注释文本,然后将其回发到我的jQueryAddComment控制器动作中,并传入一些变量。

In my controller I now have; 现在在我的控制器中;

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);

}

The callback in the above jQuery code calls a normal Javascript function to take the returned HTML and display it on the page. 上面的jQuery代码中的回调函数调用普通的Javascript函数来获取返回的HTML并将其显示在页面上。

In you case you would show a Div with the HTML in it and provide click events to it so the user can interact with it. 在这种情况下,您将显示其中包含HTML的Div并向其提供click事件,以便用户可以与其进行交互。

Is this any clearer? 这更清楚吗?

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

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