How to pass query string parameter with ActionLink in MVC

Below is my Razor syntax. I am trying to pass the s.StudentId via query string. The first <div> tag works fine, and I wanted to practice a little bit with ActionLink.

When the page loads in the browser, the links generated are:

1st div: http://localhost:57707/Students/Edit/?_studentId=1
2nd div: http://localhost:57707/Students/Edit?Length=8

What am I doing wrong inside the ActionLink, and why is it defaulting to Length=8?

@foreach (Student s in studentList)
{
    <div>@s.Email <a href="/Students/Edit/?_studentId=@s.StudentId">Edit</a></div> // Using query string to pass params

    <div>@s.Email @Html.ActionLink("Edit", "Edit", "Students", new { _studentId = s.StudentId })</div>}
}

Open in new window

LVL 8
pzozulkaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin HopkinsSenior Web DeveloperCommented:
Ok, the number of parameters you are passing(passing the controller) are causing the link to be constructed with your route attribute in the html attribute position. The easiest solution in your case is to add a null parameter to the end, also if you need to assign a css class or other property instead of the null.
@Html.ActionLink("Edit", "Edit", "Students", new { _studentId = s.StudentId }, null)

Open in new window


Hope this helps,
Dustin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Najam UddinCommented:
<div>@s.Email <a href="/Students/Edit/?_studentId=@s.StudentId">Edit</a></div> // Using query string to pass params

Should be

<div>@s.Email <a href="/Students/Edit?_studentId=@s.StudentId">Edit</a></div> // Using query string to pass params

can you post controller method for Edit of student, I think you have a parameter name length over there.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.