2013-07-04 15 views
1

我有一個文本框的視圖,當我輸入和輸入服務號碼,那麼它應該從數據庫中檢索數據並在同一視圖中顯示標籤中的那些,此應用程序是ASP.net MVC應用程序。有人能告訴我如何做到這一點。由於如何調用在asp.net mvc的控制器方法,並顯示在視圖中的標籤細節

進一步 我可以叫無javascript

控制器方法是不是可以調用視圖控制器的方法,並顯示在同一視圖 的結果。如果可以告訴我怎麼做,謝謝

+0

嗨。 http://stackoverflow.com/help/asking:請提出一個很好的問題通過幫助中心讀取。特別是,請閱讀「我如何提出一個好問題?」 –

回答

0

你可以使用AJAX。讓我們有一個例子:

@Html.LabelFor(x => x.FooBar, htmlAttributes: new { id = "fooBarLabel" }) 
@Html.TextBoxFor(x => x.FooBar, new { id = "fooBar", data_url = Url.Action("CalculateValue") }) 

,然後在一個單獨的JavaScript文件,你可以訂閱.change事件的文本字段,並觸發AJAX調用控制器動作:

$(function() { 
    $('#fooBar').change(function() { 
     var url = $(this).data('url'); 
     var value = $(this).val(); 
     $('#fooBarLabel').load(url, { value: value }); 
    }); 
}); 

和所有的左邊是相應的控制器動作:

public ActionResult CalculateValue(string value) 
{ 
    // The value parameter will contain the text entered by the user in the text field 
    // here you could calculate the value to be shown in the label based on it: 

    return Content(string.Format("You entered: {0}", value)); 
} 
相關問題