I have this action method:
[HttpPost]
public ActionResult GetNextImage(int m_id)
{
...
return Json(new{...});
}
I invoke it like this:
$(function () {
$('#nextImage').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: "m_id=" + $('#img').attr('alt'),
success: function (result) {
$('#img').attr("src", result.imagePath);
$('#img').attr("alt", result.ImageId);
}
});
return false;
});
});
I have Image object
public class Image
{
public int ImageId {get;set;}
public string imagePath {get;set;}
public List<Comment> Comments {get;set;}
}
Now. Is it possible to return from action method my “Image” object and bind it?
I don’t know how to load list of comments with JSon. Thats why I want to return object and with simple loop to display all comments. But I don’t know how to return object from action method and bind it to (razor) page.
To answer you question simply, yes you can bind your image object and return the object. Please see code below. I simplified my example slightly but I think it provides a sufficient example that you can review and modify for your own purposes. For instance instead of using a form like your original question, I am just using a button and binding the click event to call the ImageController’s GetImage method.
View
Controller
Models
Hope this helps