2017-02-24 70 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

相关问题