Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:22:39+00:00 2026-05-26T20:22:39+00:00

I have this action method: [HttpPost] public ActionResult GetNextImage(int m_id) { … return Json(new{…});

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:22:40+00:00Added an answer on May 26, 2026 at 8:22 pm

    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

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Sandbox.Models.Image>" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Image Test</title>
    
    <script type = "text/javascript" src="../../Scripts/jquery-1.5.1.min.js"></script>
    <script type = "text/javascript">
        $(document).ready(function () {
            $('#nextImage').click(function () {
                $.ajax({
                    url: "Image/GetImage",
                    type: "post",
                    data: "m_id=" + $('#img').attr('alt'),
                    success: function (image) {
                        $('#img').attr("src", image.ImagePath);
                        $('#img').attr("alt", image.ImageId);
    
                        //here is where we loop over the list of comments
                        //associated with the Image JSON object that is returned
                        //from the controller.  here 'val' is the Comment model
                        //and .Data simply calls the string member containing the
                        //actual comment
                        $.each(image.Comments, function (index, val) {
                            $('#comments').append("<div>" + val.Data + "</div>");
                        });
                    }
                });
            });
        }); 
    </script>
    </head>
    <body>
        <img alt="1" id="img" src=""/>
        <button id="nextImage">Next</button>
        <div id="comments"></div>
    </body>
    </html>
    

    Controller

     public class ImageController : Controller
     {
        public ActionResult Index()
        {
            return View();
        }
    
        [HttpPost]
        public ActionResult GetImage(int m_id)
        {
            Image image = new Image {
                ImageId = m_id, 
                ImagePath = "Content/mandrill.png", 
                Comments = new List<Comment>
                                 {
                                     new Comment {Data = "I love it"},
                                     new Comment {Data = "I love it also!"}
                                 }};
    
            return Json(image);
        }
    
    }
    

    Models

    public class Image
    {
        public int ImageId { get; set; }
        public string ImagePath { get; set; }
        public List<Comment> Comments { get; set; }
    }
    
    public class Comment
    {
        public string Data { get; set; }
    }
    

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC controller that has this Action Method: [HttpPost] public ActionResult SubmitAction()
I have an [HttpPost] action method signature like this: [HttpPost] public ActionResult Edit(ExistingPostViewModel model)
I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult
I have a method... [HttpPost] public ActionResult Start(SomeViewModel someViewModel) { ... } that based
I have an MVC Action..... [HttpPost] public ActionResult DoStuff(string myString, DateTime myDateTime) ...and I'm
I have methods like this: [HttpPost] public ActionResult Delete(BaseViewModel vm) { public ActionResult Delete(string
I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
I have an Action Method that returns a JSON-serialized object. The problem I'm having
I am using ASP.NET MVC 3. I have an action method called New. When
I have this code: function submitTwice(f){ f.action = 'http://mydomain.com/threevideopage.php'; f.target='name'; f.submit(); } I left

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.