2011-09-26 50 views
1

我一直面臨的一個問題爲前在控制器如何顯示「消息盒子」使用MVC3控制器

的一些代碼執行後,會顯示「信息箱」: -

public ActionResult IsLoginExsit(CustomerDO loginData) 
     { 

      JsonResult jsonResult = new JsonResult();    
      if (!string.IsNullOrEmpty(loginData.UserName) && !string.IsNullOrEmpty(loginData.Password))     
      {      
       bool result = Businesss.Factory.BusinessFactory.GetRegistrations().IsLoginExist(loginData.UserName, loginData.Password);      

        jsonResult.Data = result; 

      }  
      return jsonResult; 
     } 

在看到上面的例子,如果結果是真或假,那麼我想顯示消息框,說明登錄是成功或失敗。

<!-- language: lang-.html --> 
    <form class="formStyle" action="#" method="POST" id="frmAuthenticate"> 
      <div class="row"> 
      <input class="text row" type="text" value="" id="txtUserName"/> 
      </div> 
      <div class="row"> 
      <input class="text row" type="password" value="" id="txtPassword" /> 
      </div> 
      <div class="row last"> 
      <a class="link" href="">Forgot Password?</a> 
      <a class="button3" href="/Registration/Registration" title="Registration" >Signup</a>   
<input type="submit" class="button4" id="btnGo" value="Go" />    
      </div> 
     </form> 

如果登錄存在,我想定位到「/客戶/ CollaborationPortal」,否則我想顯示消息「Authroization失敗」。

$("#btnGo").click(function (e) { 
       var RegData = getRegData(); 
       if (RegData === null) { 
        console.log("Specify Data!"); 
        return; 
       } 
var json = JSON.stringify(RegData) 
       $.ajax({ 
        url: '/Registration/IsLoginExsit', 
        type: 'POST', 
        dataType: 'json', 
        data: json, 
        contentType: 'application/json; charset=utf-8', 
        success: function (data) {      
        if(data.result == true){             
        location.href = "/Customer/CollaborationPortal";      
        }      
        else{ 
         alert("Login failed"); //or whatever      
        }    
        } 
       }); 
return false; 
      }); 
      function getRegData() { 

       var UserName = $("#txtUserName").val(); 
       var Password = $("#txtPassword").val(); 
       return { "UserName": UserName, "Password": Password }; 
      } 

在此先感謝

+0

You are missing; var json = JSON之後。stringify(RegData) ,你沒有更新控制器代碼 –

+0

嗨,我已經更新了我的控制器代碼。 – Pardha

+0

怎麼樣; ? 如果你將結果設置爲jsonResult.Data,那麼在你的成功函數中,你應該檢查if(data == true)。如果您使用我的答案中的客戶端代碼,那麼您還應該使用服務器代碼。 –

回答

3

有沒有辦法做到這一點在MVC一樣簡單的WinForms應用程序。

到你的情況,網頁上顯示的消息框

簡單的方法是改變從的ActionResult這個動作JsonResult,並更換,如果用:

return Json(new {result = result}); 

,並在網頁上,你需要使用AJAX(即提交使用窗體jQuery的$。員額),並在回調函數檢查結果:

$("form input[type=submit]").click(function(){ 
    var formData = $(this).closest("form").serialize(); 
    $.post("urltoyourcontrollerhere/IsLoginExsit", formData, function(data){ 
     if(data && data.result == true){ alert("Login exists!");} 
    }); 
}); 

UPDATE 您發佈的代碼看起來不錯,但有一個問題。成功函數:

success: function (data) { 
         location.href = "/Customer/CollaborationPortal"; 
        } 

此功能將始終執行重定向,不管返回什麼控制器。你需要檢查data.result(如果你返回的Json爲Json(new {result = result});)爲true,然後重定向,否則顯示alert。因此,嘗試:

success: function (data) { 
        if(data.result == true){       
         location.href = "/Customer/CollaborationPortal"; 
        } 
        else{ 
         alert("Login failed"); //or whatever 
        } 
      } 

另一件事:

  var RegData = getRegData(); 
      if (RegData === null) 

如果你想要這個工作,你需要從getRegData返回null時,文本框的一個爲空。

+0

$(this).closest(「form」)。serialize()返回「」,請讓我知道你是否可以解決它。 – Pardha

+0

你可以發佈你正在點擊的窗體/按鈕的html來運行這個控制器動作,我會相應地更新答案。 $(this).closest(「form」)是包含你點擊的按鈕的jQuery選擇器。你的按鈕可能不在表單等內,它是你從瀏覽器 - >查看源代碼複製html的最佳選擇。 –

+0

嗨,我已在主標籤中更新..感謝您的幫助。 – Pardha

1

您將無法顯示使用服務器端代碼的消息框。您需要將一些數據傳回到指示登錄狀態的視圖,然後編寫一些客戶端代碼以顯示消息框。

+0

請讓我知道一些例子,如果可以的話,因爲它對我來說非常緊迫。在此先感謝 – Pardha

1

你可以使用,這將被傳遞到視圖和視圖模型的屬性將包含這樣的信息:

var model = new MyViewModel(); 
bool result = Businesss.Factory.BusinessFactory.GetRegistrations().IsLoginExist(loginData.UserName, loginData.Password); 
model.IsLoginSuccess = result; 

... 

return View(model); 

和強類型視圖中,你會測試這個屬性和顯示的值因此消息:

@if (Model.IsLoginSuccess) 
{ 
    <div>The Login was successful</div> 
} 
+0

我沒有在我的項目中使用「模型」,因爲我在業務解決方案中創建實體。請讓我知道,如果你可以用一些其他的邏輯來解決它 – Pardha

+1

@Pardha,那麼,你應該使用模型。這是推薦的做法。 –

3

完成後,您可以顯示一個消息框。

public ActionResult LoginExist(BAMasterCustomerDO loginData) 
{ 
    return new JavascriptResult { Script = "alert('Saved successfully');" }; 
} 
相關問題