Tengo un error cuando intento usar la búsqueda. El diccionario de parámetros contiene una entrada nula para el parámetro 'id' de tipo no anulable System.Int32' for method 'System.Web.Mvc.ActionResult.Un parámetro opcional debe ser un tipo de referencia, un tipo anulable o debe declararse como un parámetro opcional. Esto es mi controlador:
public class PersonController : Controller {
public ActionResult Buscar(int id)
{
var mm = DataBase.getById(id);
return View(mm);
}
}
Mi clase DataBase:
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;}
Mi vista Buscar:
@using (Html.BeginForm("Buscar", "Person",FormMethod.Get))
{
<form>
Title: @Html.TextBox("id");
<input type="submit"
name="name"value="Buscar"/>
</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>
}
El routeConfig:
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 }
);
}
}