views:

90

answers:

3

Hi,

I have a Create ActionMethod, something along the lines of:

[AcceptVerbs(HttpVerbs.Post)]
public ActionMethod Create(Journey journey)
{
  if (Request.IsAjaxRequest())
  {
    //Save values
    return Json(new { JourneyID = journey.JourneyID } );
  }
}

The Journey object that I pass in is from my LINQ2SQL datamodel. I call the above ActionMethod from my Javascript using the JQuery.Post function, like:

var journeyData = {
    CustomerID: $('#CustomerID').val(),
    JourneyID: $('#JourneyID').val(),
    EstimatedTravelTime: $('#EstimatedTravelTime').val(),
    RouteName: $('#RouteName').val(),
    .....
};

$.post('/Journey/Create',
  journeyData,
  function(jsonResult) {
    //deal with result
  },
  'json'
);

The issue that I am having is that in the ActionMethod Journey.RouteName is always returning as null but the JSON that I pass back has a value, I check this using

alert(JSON.stringify(journeyData));

and in the resultant JSON object RouteName has a value, e.g. 'Go to work'. Any ideas why this wouldn't be being set in the ActionMethod? All other values that I pass back are being set fine.

A: 

Have you examined your request JSON object that's going to the server?
Have you tried adding quotes to your string property value?
Like this:

var journeyData = {
    CustomerID: $('#CustomerID').val(),
    JourneyID: $('#JourneyID').val(),
    EstimatedTravelTime: $('#EstimatedTravelTime').val(),
    RouteName: '"' + $('#RouteName').val() + '"',
    .....
};
Robert Koritnik
Hey, just tried your suggestion but it's still coming back as null. It is the only value that is a string tho, the rest are bool, int or DateTime... could be something else string related?
Fermin
And to answer your first question, just before sending the journeyData variable to the server I use JSON.stringify(journeyData) to check what is being sent, is there another way to examine what is ACTUALLY being sent?
Fermin
Firebug's Net panel or Fiddler will show you.
Craig Stuntz
Using Firebugs Net panel ths RouteName is being passed back, I checked the Post tab and it has a value of "Local+Club" for the one I just tried, so it looks like it's being sent. Could it be a setting with my LINQ2SQL model?
Fermin
Can you try with "LocalClub" just in case it has something to do with UrlEncoding?
Malcolm Frexner
Firebug's Console window will show all Ajax requests after the page has loaded. And while doing it, could you please send the Request JSON object as it's sent to the server...
Robert Koritnik
+2  A: 

Just a try and error suggestion:

First thing i would try is to rename "RouteName" param with somethign different, as "RouteName" is also a property of some MVC-redirect methods..

David
This sounds likely to me
JonoW
A: 

There are a couple of things to consider. I'm guessing this is already in place, but your model must have a getter and setter on the RouteName property in order to be properly bound. Another thought is you might not be establishing the strong binding. This is usually done as part of the view's page declaration (e.g. Inherits="System.Web.Mvc.ViewPage") but I'm not sure this is happening prior to your post.

Ben Griswold