I am new to MVC
. In My Application , I\'m Retrieving the Data from Mydatabase. but when I run my Application it show Error Like This
this is my url
<
I faced this error becouse I sent the Query string with wrong format
http://localhost:56110/user/updateuserinfo?Id=55?Name=Basheer&Phone=(111)%20111-1111
------------------------------------------^----(^)-----------^---...
--------------------------------------------must be &
so make sure your
Query String
or passed parameter in the right format
It appears that you are using the default route which is defined as this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The key part of that route is the {id}
piece. If you look at your action method, your parameter is k
instead of id
. You need to change your action method to this so that it matches the route parameter:
// change int k to int id
public ActionResult DetailsData(int id)
If you wanted to leave your parameter as k, then you would change the URL to be:
http://localhost:7317/Employee/DetailsData?k=4
You also appear to have a problem with your connection string. In your web.config, you need to change your connection string to this (provided by haim770 in another answer that he deleted):
<connectionStrings>
<add name="EmployeeContext"
connectionString="Server=.;Database=mytry;integrated security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
It seems that your action needs k
but ModelBinder can not find it (from form, or request or view data or ..)
Change your action to this:
public ActionResult DetailsData(int? k)
{
EmployeeContext Ec = new EmployeeContext();
if (k != null)
{
Employee emp = Ec.Employees.Single(X => X.EmpId == k.Value);
return View(emp);
}
return View();
}
Also make sure the value is not too large or too small for int like in my case.
I am also new to MVC and I received the same error and found that it is not passing proper routeValues
in the Index
view or whatever view is present to view the all data.
It was as below
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
I changed it to the as show below and started to work properly.
<td>
@Html.ActionLink("Edit", "Edit", new { EmployeeID=item.EmployeeID }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
Basically this error can also come because of improper navigation also.