0

I have view with button, and when the button is clicked partial view is loaded.(see code below), in some reason my .load function passes the object but Name is null and Id is 0

My View controller method:

public TestModel DoSomething()
    {
        TestModel m = new TestModel();
        m.Name = "dosomething";
        m.Id = 3;

        return m;
    }

My partial view Controller Action:

public ActionResult _Products(TestModel m)
    {           
        return PartialView("_Products", m);
    }

my jQuery:

$(document).ready(function () {
$(".example").click(function (e) { 
    e.preventDefault();
    $.ajax({
        url: "DoSomething/",
        async: false,
        success: function (data) {
            $("#partialViewPlace").load("/Test/_Products", data);
        }
    });

});
});

When I debug the data parameter is passed but name is null and id is 0

9
  • You're not passing data to load() - that's the callback handler. Also making two AJAX requests seems redundant. Just make one and return the HTML you need from that. Also, remove async: false. It's horrendous practice. If you check the console you'll even see the browser giving you a warning telling you not to use it Commented Apr 25, 2017 at 13:19
  • @RoryMcCrossan api.jquery.com/load .load( url [, data ] [, complete ] ) - looks like it's the correct usage (although not one I'd use myself) Commented Apr 25, 2017 at 13:31
  • From further down that page "The POST method is used if data is provided as an object; otherwise, GET is assumed" - so try changing your action to public ActionResult _Products([FromBody] TestModel m) Commented Apr 25, 2017 at 13:32
  • @Grizabela try changing return type of DoSomething method to ActionResult and returning a Json(m) Commented Apr 25, 2017 at 13:51
  • Thank you, but nothing worked. I want to pass data to partial view from different controller, this is why I am doing this. If you can give me idea how to do it in different way? Commented Apr 25, 2017 at 14:03

0

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.