0

I am making an ajax call using the following code.

$.ajax(  
    {  
        url: '/BuildingCompany/EditAjax/' + id,  
        cache: false,  
        type: 'POST',  
        data: ({ imageValue: $(this).attr("src") }),  
        contentType: 'application/json; charset=utf-8',  
        dataType: 'json'  
}  

In the EditAjax action, how should I retrieve data send by this ajax call.

0

3 Answers 3

2

Once the ajax form is submitted, ASP.NET MVC model binder will match the POST parameter "imageValue" to the action method parameter.

Assuming your routes are set up properly, the below should work:

public class HomeController {
    [HttpPost]
    public ActionResult EditAjax(string companyName, string imageValue) {
       //companyName == "BuildingCompany"
       //imageValue == "The Image source"
    }
}

The route setup should look something like this:

routes.Add(new Route("{companyName}/{action}",
    new RouteValueDictionary { { "controller", "Home" } },
    new UIRouting()));
Sign up to request clarification or add additional context in comments.

Comments

0
[HttpPost]
public JsonResult EditAjax(string companyName, string imageValue) {
   // do stuff here


   return Json(object);
}

Comments

0

Igor's solution would work if you were using AJAX to send a form normally but you are sending JSON so an extra step is needed.

In the MVC futures library (available on CodePlex) is a JSONValueProvider. Download the library and reference Microsoft.Web.Mvc.

In your Global.asax.cs Application_Start() method add the line:

ValueProviderFactories.Factories.Add(new Microsoft.Web.Mvc.JsonValueProviderFactory());

This will let your action methods model bind against JSON as you would normally. You might already know this but as you are using JSON you can return

Json(object)

Which will serialise your object to JSON, send correct content-type etc for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.