Just Wondering我可以将我的动作链接变量分配给下拉列表的选定值,还是必须使用jquery分配它。我有以下示例,但只是想知道是否有更好的方法。
这是我的观点
@{
ViewBag.Title = "Index";}
@section head{
<script type="text/javascript">
$(document).ready(function () {
//begin populate the select list with A-Z
for (var i = 0; i < 26; i++)
$('<option value=' + String.fromCharCode((65 + i)) + '>' + String.fromCharCode((65 + i)) + '</option>').appendTo($('#select'));
//end populate the select list with A-Z
//code for clicking the filter link
$('.link a').click(function (e) {
e.preventDefault();
var firstletter = $('#select').val(); //get the select value of the drop down
var url = $(this).attr('href').replace("1", firstletter); //correct the url of the action link (Do I need to do this? or can I do it directly in the action link?)
//call the controller Filter function
$.getJSON(url, function (data) {
$('#presidents').empty(); //empty out the ul
//populate the ul with the filtered list
$.each(data, function (index, president) {
$('<li>' + president + '</li>').appendTo($('#presidents'));
});
});
});
});
</script>
}
<h2>Presidents</h2>
<select id='select'>
<option value="all">all</option>
</select>
<li class="link">@Html.ActionLink("Filter", "Filter", new { firstletter = "1" })</li>
<ul id="presidents">
@foreach (var president in Model)
{
<li>@president</li>
}
</ul>
这是我的控制器
namespace advancedviewtecniques.Controllers{
public class PresidentController : Controller
{
private readonly string[] _presidents = {"Adams", "Arthur", "Buchanan", "Bush",
"Carter", "Cleveland", "Clinton", "Coolidge",
"Eisenhower", "Fillmore", "Ford", "Garfield",
"Grant","Harding", "Harrison", "Hayes", "Hoover",
"Jackson", "Jefferson", "Johnson", "Kennedy",
"Lincoln", "Madison", "McKinley", "Monroe",
"Nixon", "Pierce",
"Polk", "Reagan", "Roosevelt", "Taft",
"Taylor", "Truman", "Tyler",
"Van Buren", "Washington", "Wilson"};
public ActionResult Index()
{
return View(_presidents);
}
public ActionResult Filter(string firstletter)
{
if (firstletter != "all"){
IEnumerable<string> filteredlist = _presidents.Where(p => p.StartsWith(firstletter));
return Json(filteredlist, JsonRequestBehavior.AllowGet);
}
return Json(_presidents, JsonRequestBehavior.AllowGet);
}} }