Austin Harris
Austin Harris

Reputation: 5410

MVC4 ActionLink syntax for use with hidden text box values

What is the secret syntax for passing in two values from two hidden text boxes to a controller method? I tried the syntax below but I get: CS0103: The name 'SearchLatitude' does not exist in the current context error message. It doesn't see or like my hidden text box named SearchLatitude. Not sure what to do, thank you.

@Html.ActionLink("SORT BY DISTANCE", "DisplayJobsDistance", "Job", new { Latitude = SearchLatitude, Longitude = SearchLongitude }, null)

<input type="hidden" name="SearchLatitude" id="SearchLatitude"> 
<input type="hidden" name="SearchLongitude" id="SearchLongitude"> 

Upvotes: 2

Views: 5211

Answers (2)

Simon Halsey
Simon Halsey

Reputation: 5480

Your ActionLink command won't work because it's looking for a property in the view called SearchLatitude. You need to add this to the Model that is passed to the view. If you don't know these values, maybe because they're added in the client be JavaScript, then some jQuery will help.

@Html.ActionLink("SORT BY DISTANCE", "DisplayJobsDistance", "Job", null, new {id="sortlink"})

and

$("#sortlink").click(function()
{
    var $lat = $("#SearchLatitude").val();
    var $lon = $("#SearchLongitude").val();

    $(this).attr("href", $(this).attr("href") + "?latitude=" + $lat" + "&longitude=" + $lon);
}

Upvotes: 2

Erwin
Erwin

Reputation: 3090

I've used:

@model Person
@Html.HiddenFor(i => i.Id)
@Html.ActionLink("Person", "Edit", new { id = Model.Id })

Upvotes: 0

Related Questions