Reputation: 725
I am trying to store json location value in tempdata, I am getting the user current location by using json api and I want to display the content to user on the base of location but I couldn't be able to do this so I am sharing my code, guys please help me out
<script type="text/javascript">
var strip, strcountry, strcity, strregion, strlatitude, strlongitude, strtimezone
function GetUserInfo(data) {
strip = data.host; strcountry = data.countryName; strcity = data.city;
strregion = data.region; strlatitude = data.latitude; strlongitude = data.longitude;
strtimezone = data.timezone;
}
$(function () {
BindUserInfo();
})
function BindUserInfo() {
//document.getElementById('lblCity').innerText = strcity;
$('#term').attr("placeholder", strcity);
$('#term').attr("value", strcity);
}
</script>
<script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>
now I want to get the location from here and apply in linq query, I am using MVC 4 (Razor) please help me out of this.
Upvotes: 0
Views: 1939
Reputation: 219047
Based on the comments above, it sounds like this sequence of events is happening:
If that's correct, then you have a number of options available to you. To be clear, what is the value in question? strcity
?:
$('#term').attr("placeholder", strcity);
$('#term').attr("value", strcity);
If so, then when you set that value here you could also potentially set it on perhaps the query string for the link to the next page. Something like this:
$('#someLink').src = '@Url.Action("Index", "Restaurants")?city=' + strcity;
This would use a mix of pre-run server-side code for the root of the link, and client-side code to just append that one value to it. Then the controller method simply accepts city
as a parameter:
public ActionResult Index(string city)
{
// get your list of restaurants and return the view
}
There's no need for TempData
in this, you're just sending the value directly to the controller action.
If for whatever reason you don't want to modify the src
like that, you could perhaps instead send the value through a form invoked by the link. So instead of an ActionLink
to the index action on the restaurants controller, you might have something like this:
@using(Html.BeginForm("Index", "Restaurants", FormMethod.Get))
{
<a id="restaurantsLink" href="javascript:void(0);">Go to restaurants</a>
<input type="hidden" name="city" />
}
And JavaScript code later:
$('#restaurantsLink').click(function () {
$('form').submit();
});
You might need to more uniquely identify that form in the jQuery selector, particularly if there are (or could be) other forms on that page. Then in your original JavaScript code you would just set the value in the hidden input:
$('input[name=city]').val(strcity);
In this second case the controller action would still be the same as the first one, accepting a city
string parameter.
Upvotes: 1