2012-04-26 3 views
1

私はMVC3を初めて使用しました。値をモーダルダイアログに戻すことができるかどうかを知りたいと思います。以下の例を参照してください。MVC3のモーダルダイアログへの戻り値

私の.cshtmlファイルでは、「ログ」ボタンをクリックするとログアクションが呼び出されます。私のコントローラのアクションで

<button name="button" value="log" id="log">Log</button> 
<div id="dialog-message" title="Input Error!"> 
    <p> 
    <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span> 
    The result from the controller is : 
    </p> 
</div> 

私はjqueryのモーダルダイアログがxの値を表示するこの

public ActionResult Log(FormCollection collection) 
{ 
    if(x+2!=4) 
    { 
     // return the value of x to the modal dialog 
    } 
    else 
    { 
     // save record to database 
    } 
} 

を持っています。 jqueryのスクリプトで

、私は助けてください

$("#dialog-message").dialog({ 
     autoOpen: false, 
     modal: true, 
     buttons: { 
      Ok: function() { 
       $(this).dialog("close"); 
      } 
     } 
    }); 

    $("#log").click(function() { 
     $("#dialog-message").dialog("open"); 
     this.defaultShowErrors(); 
    }); 

の下を持っています!

答えて

2

あなたには2つの欠点があります:1)結果のコンテナ、2)結果を得るためのAjax呼び出し。第一容器:

The result from the controller is : <span id='result_container'></span> 

そしてAjax呼び出し:

$("#log").click(function() { 
    var ajaxUrl = // set the URL for **Log** here 
    $.get(ajaxUrl, function(data) { 
     $("#result_container").html(data); 
    }); 
    $("#dialog-message").dialog("open"); 
}); 

コントローラでは、あなただけのプレーンテキストとして結果を返したい:すべての

if(x+2!=4) 
{ 
    // return the value of x to the modal dialog 
    return new ContentResult() { 
     Content = x, 
     ContentEncoding = System.Text.Encoding.UTF8, 
     ContentType = "text/plain" 
    }; 
} 
+0

解決策を試したところ、モーダルダイアログは空でした。しかし、xの値を含む新しいページが開きました。私が必要とするのは、ダイアログにxを表示することです。ありがとうございました –

関連する問題