我想基于视图中的用户选择将参数发送回控制器。
这是我的看法:
@model IEnumerable<Users>
@{
ViewBag.Title = "FieldSaver Users";
}
<h2>Users</h2>
@foreach(char c in ViewBag.Alphabet)
{
@Html.ActionLink(c.ToString(), "Home/" + c, new object { }, new { @class = "alphabet", @letter = c })
}
<div class="table-responsive">
<table class="table table-striped">
<tr>
<td>Username</td>
<td>First name</td>
<td>Last name</td>
</tr>
@foreach (var user in Model)
{
<tr>
<td>@user.UserName</td>
<td>@user.FirstName</td>
<td>@user.LastName</td>
</tr>
}
</table>
</div>
这是控制器:
public ActionResult Home(string letter)
{
ViewBag.Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
using (var db = new App.Models.DbContext())
{
List<Users> users = (from p in db.Users
select p).ToList();
return View(users);
}
return View();
}
因此,当有人单击字母链接中的字母时,我想将值发送回控制器,当前正在回发,但letter参数为null。
我究竟做错了什么?
您当前使用的new object { }
是提供路线数据的位置。您正在将其添加为HTML属性。
在您的情况下,应该是这样的:
@Html.ActionLink(c.ToString(),
"Home",
new { letter = c },
new { @class = "alphabet" })
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句