Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my code is
public ActionResult EditData(string id)
{
try
{
using (SqlConnection con = new SqlConnection(connStr))
{
string sql_cmd = "select UserId,UserName,FirstName,LastName from UserDetails where UserId=@id";
SqlCommand cmd = new SqlCommand(sql_cmd, con);

}
catch (Exception)
{

}
return View();
}

and view is


@using (Html.BeginForm(FormMethod.Post))
{

@grid.GetHtml(tableStyle:"webGrid",
headerStyle: "header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("LastName","Last Name",format:@
@item.last_name
),
grid.Column("View Details", header: "Edit", format: @

@Html.ActionLink("Edit","EditData","UserInfo", new {id=item.user_id})

),grid.Column("Delete", header: "Delete", format: @

@Html.ActionLink("Delete",null,null, new { username = item.user_id })

)))


}

in controller i am getting null value in ID how do i get ID in controller
Posted
Updated 23-Feb-15 2:32am
v2

You have not set the parameter for @id

string sql_cmd = "select UserId,UserName,FirstName,LastName from UserDetails where UserId=@id";


Also you should be making the Model classes from the SQL database via Entity Framework, then reference the Model class for UserDetails with DbContext or similar methods.

Read tutorials here: http://www.asp.net/mvc[^]
 
Share this answer
 
First the parameter expected by the action, must cause routing problems, because is configured by default for HttpGet request. But when you do a submit of html form, the parameters send to the server is serialized different.

You should try this way:

[HttpGet]
public ActionResult EditData(int id)
{
try
{
using (SqlConnection con = new SqlConnection(connStr))
{
string sql_cmd = "select UserId,UserName,FirstName,LastName from UserDetails where UserId=@id";
SqlCommand cmd = new SqlCommand(sql_cmd, con);
cmd.Parameters["@id"].Value = id;
//TODO: ...
}
}
catch (Exception)
{
 
}
return View();
}


and view is


@using (Html.BeginForm(FormMethod.Get))
{

	@grid.GetHtml(tableStyle:"webGrid",
	headerStyle: "header",
	alternatingRowStyle: "alt",
	selectedRowStyle: "select",
	columns: grid.Columns(
	grid.Column("LastName","Last Name",format:@
	@item.last_name
	),
	grid.Column("View Details", header: "Edit", format: @
	@Html.ActionLink("Edit","EditData","UserInfo", new {id=item.user_id}, null)

	),grid.Column("Delete", header: "Delete", format: @
	@Html.ActionLink("Delete",null,null, new { username = item.user_id }, null)

	)))
}


More help:
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters%28v=vs.110%29.aspx[^]
http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900