2016-11-12 85 views
0

我有一個問題,從視圖製作jQuery AJAX來調用控制器的動作。我的實驗是驗證2個文本框中的電子郵件地址:「電子郵件地址」和「電子郵件密碼」。我想將結果更新到2個文本框下的「div」。請在下面看到我的代碼更多:ASP.NET - 如何使用jQuery AJAX調用控制器的動作

我的控制器:

[HttpPost] 
[ValidateAntiForgeryToken] 
public string verifyEmail(string emailAddress,string emailPassword) 
{ 
    ImapClient ic; 
    string result; 
    try 
    { 
     ic = new ImapClient("imap.gmail.com", emailAddress, emailPassword, AuthMethods.Login, 993, true, false); 

     //if the email address is verified 
     result = "OK"; 
    } 
    catch 
    { 
     //else 
     result = "Failed"; 
    } 

    //the result will be inserted into a HTML <div> 
    return result; 
} 

我的觀點:

<div class="form-group"> 
     @Html.LabelFor(model => model.emailAddress, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.emailAddress, new { htmlAttributes = new { @class = "form-control", @id = "email-address-textbox" } }) 
      @Html.ValidationMessageFor(model => model.emailAddress, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.emailPassword, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(model => model.emailPassword, new { @class = "form-control", @id = "email-password-textbox" }) 
      @Html.ValidationMessageFor(model => model.emailPassword, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

//click on this link to verify the email 
      @Html.ActionLink("Verify",null,null, htmlAttributes: new { @id = "email-verify" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-10"> 
      <div id="email-verification-status"> 
       //the result will be updated here 
      </div> 
     </div> 
    </div> 

而且我的jQuery AJAX:

$(document).ready(function() { 
        function verify() 
        { 
         $.ajax({ 
          url: "/Ultility/verifyEmail", 
          type: "POST", 

          //get 2 values from the 2 text boxes 
          data: { emailAddress: $('#email-address-textbox').val(), emailPassword: $('#email-password-textbox').val() }, 
          contentType: "application/json; charset=utf-8", 
          dataType: "json", 
          success: function (data) { 
           //don't know what to insert here 
          } 
         }); 
        } 

        //call the AJAX function whenever the link is clicked 
        $('#email-verify').on('click', verify); 
       }); 

一切工作正常,我可以調用動作,但我不知道如何用返回的結果更新div標籤。任何幫助將appriciated!

+0

'$(「#email-verification-status」)。html(data)'should do – Developer

回答

1

你試過了嗎?

$(「#email-verification-status」)。text(data);

相關問題