2016-11-12 59 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);

相关问题