2012-05-23 36 views
2

我很努力登錄時出現登錄對話框。我需要發生的是,當用戶使用內置的MVC3身份驗證控件登錄時,我需要它彈出一個對話框,吐出一些條款和協議,並說「如果您接受,則表示您同意上述內容」。我目前有,但是當我取消時,網站仍然會登錄。這裏是我的代碼:'你接受條款和條件嗎?'在登錄表單身份驗證MVC3

<fieldset> 
    <legend>Login</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.userName) 
     </div> 
     <div class="focus"> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.userName) 
      </div> 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.passWord) 
     </div> 
     <div class="editor-field"> 
      @Html.PasswordFor(model => model.passWord) 
     </div> 

     <p> 
      <input type="submit" value="Login" name="btnLogin" id="btnLogin" /> 
     </p> 
     @Html.ValidationSummary(true) 


</fieldset> 

和JavaScript,我有:

<script type="text/javascript"> 
     $(function() { 
      $("#btnLogin").click(function() { 
       var answer = confirm("Unauthorized access or use of the web content is prohibited.\n" + "By clicking 'OK', you have agreed to the above conditions"); 
       if (answer) { 
       } 
       else { 
        $(this).dialog("close"); 
       } 
      }); 
     }); 
    </script> 

我將如何得到它僅僅取消HttpPost上登錄時,用戶點擊該對話框上取消,並繼續登錄如果他們打好了?

非常感謝!

+0

只是如此,你知道,有人仍然可以通過登錄而不檢查,因爲你你只能在客戶端檢查這個值。你應該驗證所有的輸入,不管它來自客戶端和服務器兩者 –

回答

3
在事件處理

添加此

$("#btnLogin").click(function (event) { 
    var answer = confirm("Unauthorized access or use of the web content is prohibited.\n" + "By clicking 'OK', you have agreed to the above conditions"); 
    if (answer) { 

    } 
    else{ 
      $(this).dialog("close"); 
      event.preventDefault(); 
    } 
}); 

添加event.preventDefault();你保證preveting形式

+0

這隻能幫助客戶端,並不會幫助服務器端驗證 –

+0

就是這樣!非常感謝!我無法讚揚你,直到我獲得15個聲望,但我會盡快擊中支票(它說我需要等5分鐘) – KrispyK

+0

@CDSmith它是正確的,但在問題中不指定是否需要服務器或客戶端,但你的答案也是有效的,對於服務器端 – Jorge

1

的默認行爲,爲了確保此驗證服務器端和客戶端使用該技術,作品完美。如果你只依賴於JavaScript,那麼通過解決這個問題,仍然可以登錄到你。

bool屬性稱爲AcceptTerms到模型

[Display(Name = "Accept Terms and Conditions")] 
[IsTrue] 
public bool AcceptTerms { get; set; } 

添加在你看來

@Html.LabelFor(model => model.AcceptTerms) </br> 
@Html.CheckBoxFor(model => model.AcceptTerms) </br> 
@Html.ValidationFor(model => model.AcceptTerms) 

創建一個新的屬性,以便您可以驗證檢查

public class IsTrueAttribute : ValidationAttribute 
{ 
    public override bool IsValid(object value) 
    { 
     if (value == null) return false; 
     if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties."); 

     return (bool) value == true; 
    } 
}