尝试使用搜索时出现错误。这是我的错误:
参数字典在'MvcSimpleModelBinding.Controllers.PersonController'中的方法'System.Web.Mvc.ActionResult Trazi(Int32)'中包含非空类型'System.Int32'的参数'id'的空条目。可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。
这是我的控制者:
public class PersonController : Controller {
public ActionResult Search(int id)
{
var mm = DataBase.getById(id);
return View(mm);
}
}
我的班级数据库:
public class DataBase
{
private static List<Person> people = new List<Person>();
private static int _nextId = 1;
public static List<Person> getAll() {
return people;}
public static Person getById(int id)
{
var buscar = people.Find(x => x.Id == id);
if (buscar == null)
{
throw new ArgumentNullException("id");
}
return buscar;}
查看
@using (Html.BeginForm("Search", "Person",FormMethod.Get))
{
<form>
Title: @Html.TextBox("id");
<input type="submit"
name="name"value="Search"/>
</form>
}
<p>@Html.ActionLink("Create New",
"Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>@Html.DisplayNameFor(model =>
model.Name)
</th>
<th>@Html.DisplayNameFor(model =>
model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.Street)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.State)
</th>
<th>
@Html.DisplayNameFor(model => model.Zipcode)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.Street)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.State)
</td>
<td>
@Html.DisplayFor(modelItem => item.Zipcode)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
路由:
public class RouteConfig{
public static void
RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
答案 0 :(得分:1)
该错误表示搜索请求中没有id
参数,这就是MVC甚至无法调用该操作方法的原因。
现在,让我们找出请求错误的原因。让我们看看视图中的内容:
@using (Html.BeginForm("Search", "Person",FormMethod.Get))
{
<form>
Title: @Html.TextBox("id");
<input type="submit" name="name" value="Search"/>
</form>
}
HTML帮助器Html.BeginForm
产生一个新表单(<form></form>
标签),但是随后您还添加了另一种表单,因此结果html为:
<form method="get" ...>
<form>
Title:....
</form>
</form>
这是一个问题,因为HTML不支持嵌套表单。要解决此问题,您应该在using内删除多余的<form>
标签:
@using (Html.BeginForm("Search", "Person",FormMethod.Get))
{
Title: @Html.TextBox("id");
<input type="submit" name="name" value="Search"/>
}
答案 1 :(得分:0)
您的问题在这里。
public ActionResult Search(int id)
您为操作设置了一个参数,因此每次输入操作时都需要一个参数:int id。例如,如果您从另一个页面(例如搜索页面)进入相应的视图页面(即搜索页面),索引,您要做的就是将原始值带入您的ruote值。下面是我的示例:
另一种观点:
@Html.ActionLink("Search", "Home", new { id = 2 })
该示例实际上等于/ Home / Search / 2,这是/ [Controller] / [Action] / [Route value]的格式,因此您不会再收到该错误。