views:

52

answers:

2

I don't know if I've understanding MVC correctly if my question makes no sense, but I'm trying to understand the following:

I have some code on a controller that returns JSON data. The JSON data is populated based on a choice from a dropdown box on an Asp.Net page. I thought (incorrectly) that Session variables would be shared between the Asp.Net project and the MVC Project. What I'd like to do therefore (if this is possible), is to call a Sub on the MVC that sets a variable before the JSON query is run.

I have the following:

Sub SetCountryID(ByVal CountryID As Integer)
    Me.pCountrySelectedID = CountryID
End Sub

Which I can call by the following:

Response.Write("http://localhost:7970/Home/SetCountryID/?CountryID=44")

But this then results in a blank page - again obviouslly totally incorrect!

Am I going about MVC the wrong way or do I still have a hell of a lot more learning to do?

Is this even possible to do?

A: 

If you are posting to retrieve your json object then you can post an extra field containing the CountryId, if not then just pass the countryId through as an extra query string and add an extra para on your action.

CeejeeB
A: 

The default route will handle an id argument so your action looks like this:

Code Converted from C# by hand please excuse any mistakes

Function Lookup(ByVal id as Integer) as JsonResult
     Dim db as New DbDataContext()
     Return Json(GetMyDataAndReturnObject(), JsonRequestBehavior.AllowGet)
End Function

Call it thusly (jQuery)

$.getJSON('/Controller/Lookup/' + $('#SelectId')[0].options[id].value, function(json) {
      processResult(json);
});
Cheddar
Brilliant, thanks Cheddar that's about answered it perfectly.
JasonMHirst