[英]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.