Reputation: 17
Getting Error when I try to use search. This is my error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Trazi(Int32)' in 'MvcSimpleModelBinding.Controllers.PersonController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Here is My Controller:
public class PersonController : Controller {
public ActionResult Search(int id)
{
var mm = DataBase.getById(id);
return View(mm);
}
}
My class 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;}
View
@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>
}
Routing:
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 }
);
}
}
Upvotes: 0
Views: 2160
Reputation: 544
Your problem is here.
public ActionResult Search(int id)
You set an parameter for the action so every time when you enter the action you need an parameter: int id. For example, if you enter the corresponding view page (i.e. search page) from another page, e.g. Index, what you have to do is bringing your ruote value with your original URL. Below is my example:
Another View:
@Html.ActionLink("Search", "Home", new { id = 2 })
The example is actually equals to /Home/Search/2 and this is the format, /[Controller]/[Action]/[Route value] so you will not get the error again.
Upvotes: 0
Reputation: 3013
The error means that there is no id
parameter in the search request, that is why MVC cannot even invoke the action method.
Now, let's figure out why the request is wrong. Let's see what we have in the view:
@using (Html.BeginForm("Search", "Person",FormMethod.Get))
{
<form>
Title: @Html.TextBox("id");
<input type="submit" name="name" value="Search"/>
</form>
}
Html helper Html.BeginForm
produces a new form (<form></form>
tags), but then you also add another form, so result html is:
<form method="get" ...>
<form>
Title:....
</form>
</form>
This is a problem, because nested forms are not supported in HTML. To fix it, you should remove extra <form>
tags inside the using:
@using (Html.BeginForm("Search", "Person",FormMethod.Get))
{
Title: @Html.TextBox("id");
<input type="submit" name="name" value="Search"/>
}
Upvotes: 1