asp.net-mvc return a couple of partial views onclick
I have an asp.net mvc application. When button is clicked (sub开发者_StackOverflow社区mit 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?
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>');
});
});
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!
精彩评论