views:

57

answers:

3

Hi,

I am working on a mvc project, and having problem with json.

i have created a demo project with list of colors

    public JsonResult GetResult()
    {
        List<string> strList = new List<string>();
        strList.Add("white");
        strList.Add("blue");
        strList.Add("black");
        strList.Add("red");
        strList.Add("orange");
        strList.Add("green");
        return this.Json(strList);
    }

i am able to get these on my page, but when i try to delete one color, that is when i send the following using jquery

function deleteItem(item) {
        $.ajax({
            type: "POST",
            url: "/Home/Delete/white",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            success: ajaxCallSucceed,
            dataType: "json",
            failure: ajaxCallFailed
        });
    }

the controler action

public JsonResult Delete(string Color) {}

Color always returns null, even if i have specified "/Home/Delete/white" in the url.

i know i am doing something wrong or missing something, but not able to find out what.

please can any one guide me in the right direction.

A: 

Check your routes. (Usually set in /global.asax.)

svinto
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" });
That'll only work if your parameter is named `id`. You need either create a new route that reflects your method or change your method to reflect the current routes.
svinto
+1  A: 

Try changing your url to:

"/Home/Delete?Color=white"

The reason is that there isn't a route set up to handle a string called color, like you have.

Have a look at this for info on how to create a custom route that will handle your current url format.

http://www.asp.net/mvc/tutorials/creating-custom-routes-cs

Alastair Pitts
A: 

try this above the default

routes.MapRoute("Color", "{controller}/{action}/{color}", new { controller = "Home", action = "Index", Color = "" }); 
Pharabus
tried it, still returns null
Try changing {color} to {Color}.
svinto