2011-11-20 50 views
0

我有一個投票控件,有幾個按鈕。它使用Ajax.BeginForm實施,該隱藏字段的名稱爲voteTypeHidden。任何時候任何一個投票按鈕被點擊後,它都會改變voteTypeHidden的值,以更新投出的投票類型。該表單被提交給服務器,並且使用ViewState["VoteError"]報告任何錯誤。現在,我想用我的javascript函數顯示這個錯誤ASP.NET MVC - Ajax.BeginForm - 表單提交時有條件地彈出模態對話框?

ModalDialog(button, text, fadeOut) 

如何做到這一點? 傳遞給ModalDialog函數的文本參數是ViewState [「VoteError」]是非常重要的。

順便說一句,這是沒有必要的,但我也發佈了我使用的代碼。

這裏是JavaScript:

<script type = "text/javascript"> 
    var voteClickEnabled = true; 
    function voteClicked_Set(value) { 
     var voteType = value; 
     document.getElementById("voteTypeHidden").setAttribute("value", value); 
    } 
    function voteBegin() { 
     //alert("begin"); 
     if (voteClickEnabled == false) { 
      return false; 
     } 
     else { 
      voteClickEnabled = false; 
      return true; 
     } 
    } 
    function voteEnd() { 
     //alert("end"); 
     voteClickEnabled = true; 
    } 

</script> 

這裏是視圖:

<div id = "updateVotes"> 
<table> 
<tr> 
<td></td> 
<td></td> 
<td></td> 
</tr> 
</table> 


Favorite : 
    <%=Html.Encode(ViewData["FavoriteCount"]) %> 

<%//if(((int)ViewData["VoteBits"] & 1) == 0) 
{%> 
    <% using (Ajax.BeginForm("Voting", "Vote", new {voted = ViewData["voted"], favorited = ViewData["favorited"], markedspam = ViewData["markedspam"], VotingOnId = (long)ViewData["VotingOnId"], VoteOn = (int)ViewData["VoteOn"], num_votes = 0, num_favorite = (int?)ViewData["FavoriteCount"], sdg = (int?)ViewData["sdg"], category = (int?)ViewData["category"] }, new AjaxOptions { UpdateTargetId = "updateVotes", OnBegin = "voteBegin", OnComplete = "voteEnd" })) 
     {%> 
     <%=Html.Hidden("voteTypeHidden", "temp", new { id = "voteTypeHidden" })%> 
     <%if ((bool)ViewData["voted"] == false) 
      { %> 
     <%=Html.SubmitImage("voteButton", Url.Content("/Images/thumb_up.png"), new {value = "1", onclick = "voteClicked_Set(this.value)" })%> 
     <%} 
      else 
      { %> 
     <%=Html.SubmitImage("voteButton", Url.Content("/Images/cancel.png"), new { value = "2", onclick = "voteClicked_Set(this.value)" })%> 
     <%} %> 
     <%if ((bool)ViewData["favorited"] == false) 
      { %> 
     <%=Html.SubmitImage("voteButton", Url.Content("/Images/star_off_32.png"), new { value = "3", onclick = "voteClicked_Set(this.value)" })%> 
     <%} 
      else 
      { %> 
     <%=Html.SubmitImage("voteButton", Url.Content("/Images/star_32.png"), new { value = "4", onclick = "voteClicked_Set(this.value)" })%> 
     <%} %> 
     <%if ((bool)ViewData["markedspam"] == false) 
      { %> 
     <%=Html.SubmitImage("voteButton", Url.Content("/Images/error.png"), new { value = "5", onclick = "voteClicked_Set(this.value)" })%> 
     <%} 
      else 
      { %> 
     <%=Html.SubmitImage("voteButton", Url.Content("/Images/error_go.png"), new { value = "6", onclick = "voteClicked_Set(this.value)" })%> 
     <%} %> 
    <%}%> 
<%}%> 
<div id = "testingAjax"> 
</div> 

<% if (!string.IsNullOrEmpty((string)ViewData["VoteError"])) { %> 
<script type = "text/javascript"> 
    ModalDialog("testingAjax", "asd", false); 
</script> 
<%} %> 
</div> 

回答

0

只是做普通的HTML按鈕圖像全部用同一類,但你說的數據votetype的HTML屬性=」 「

將一個隱藏的div放在頁面頂部,稱爲結果或反饋或類似的東西,基於按鈕類投票的想法:

$('.vote').live("click",function(e){ 
    var voteType = $(this).attr("data-votetype"); 
    // do post to JsonResults 
    $.ajax(
    url: "", 
    type:"POST", 
    data:{id:$("#idfield").val(),votetype:voteType}, 
    dataType:"json", 
    success: function(data){ 
     $("#feedback").html(data.Message).show(); 

    }, 
    error:function(a,b,c){ $("#feedback").html("Error description: " + b).show(); } 
    ); 
}); 

如果你打算使用Ajax的工作,你不妨用數據模型的設計工作,這是一個輸入反應JSON,示例代碼:

public class JsonResponseModel { 
     public bool Success {get;set;} 
     public string Message {get;set;} 
} 

public JsonResults CastVote(int id, int votetype){ 
     var model = new JsonREsponseModel{ Success = false }; 
     // do code in try catch. change model to reflect success and message 
     return Json(model); 
}