Question

I have an asp.net mvc application. When button is clicked (submit button) I would like to results to be displayed inside some div. I know how to do it. I have some action where I return a partial view. But when button is submitted then I get some multiple objects from db and I would like to display them all in div.

How can I achieve it?

Was it helpful?

Solution

Your action method could serialize and return them as a JSON encoded string:

public ActionResult Foo()
{
    SomeEntity[] entities = FetchEntities();
    // The JsonRequestBehavior is necessary only in ASP.NET MVC 2.0
    return Json(entities, JsonRequestBehavior.AllowGet);
}

which could be invoked like this:

$.getJSON('/home/foo', function(json) {
    $(json).each(function(index, value) {
        // SomeProperty is a property of your entity:
        $('body').append('<div>' + value.SomeProperty + '</div>');
    });
});

OTHER TIPS

Wrap all those objects in a wrapper object and pass that object to your partial view. Strongly type your partial view to the wrapper object and you are done!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top