2017-02-24 40 views
-2

我想文本框的值,控制器如何從視圖中Asp.NET MVC傳遞數據

我用Ajax.ActionLink("Search","Result",new{id="**VALUE**"}, new AjaxOptions{...})

@* This is the Index.cshtml *@ 
<input id="cityName" type="text" class="form-control" placeholder="Enter a CityName"> 

@Ajax.ActionLink("Search", "Result", new { id = "I want to get the cityName" }, new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "cityInfo", LoadingElementId = "loading" }, new { @class = "btn btn-default" }) 

<div id="cityInfo"> @When I click the actionlink , the controller will return a partialview 
    @Html.Partial("Result", Model) 
</div> 




@*This is the Result.cshtml*@ 
<p> 
    @if (@Model!=null) 
    { 
     @Model.cityInfo 
    } 
</p> 

這是index.cshtml - >> enter image description here

感謝

public PartialViewResult Result(string cityName)//this is the controller 
    { 
     CityModel city = new CityModel(cityName); 
     city.getInfo(); 
     return PartialView("Index",city); 
    } 
+2

您的代碼需要在這個問題(沒有鏈接到它的圖像)。你不能 - 你需要一個表單來提交你的文本框的值(或者一些javascript) –

+0

@StephenMuecke你能告訴我如何編寫javascript,請 – Suen

+0

你需要**編輯你的問題**來解釋更多關於你想在這裏做什麼(我最好的猜測是你想要某種'搜索'文本框,然後你點擊'搜索'按鈕,你想使用ajax來顯示基於文本框?) –

回答

-1

你可以寫一個福nction象下面這樣:

Ajax.ActionLink("Search","Result",new{id="id"}, new AjaxOptions{...}) //set temp value 

$(document).ready(function(){ 
    //init value 
    var value = $('#cityName').val(); 
    var href = $('a').attr('href').replace('id', value); 
    $('a').attr('href', href);  

}); 
+0

但是當我點擊按鈕時,控制器得到這個「#btnSearch」 – Suen

+0

但是新的{id =「btnSearch」}無法獲得btnSearch的值。它會得到一個字符串「btnSearch」 – Suen

+0

@Suen:你可以更新你的完整源代碼。所以我可以輕鬆支持。 – Tomato32

相關問題