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 8531199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:25:44+00:00 2026-06-11T09:25:44+00:00

I have a blog object which can contain comment objects. Currently, the blog contains

  • 0

I have a blog object which can contain comment objects.

Currently, the blog contains the blog info and also a partial that renders a list of comments and the form to add a new comment. This comment form posts to a Save action in my Comment controller and then redirects back to the Show action of the Blog controller. The below code is the action action.

        //the id of the blog this comment is for
        int blogId;
        bool parsed = Int32.TryParse(Request.Form["blogId"].ToString(), out blogId);

        //get the blog
        BlogService blogService = new BlogService(unitOfWork);
        Blog blog = new Blog();

        if (parsed)
        {
            blog = blogService.GetBlogArticle(blogId);
        }

        //setup up our comment
        CommentService service = new CommentService(unitOfWork);
        Comment comment = model;
        comment.Blog = blog;

        //set the comment date
        comment.PostedDate = DateTime.Now;

        bool actionSucceeded = true;
        string errorMessage = "Something went wrong. Please try your request again.";

        try
        {
            service.Add(comment);
            unitOfWork.Save();  
        }
        catch
        {
            actionSucceeded = false;
        }

        if (actionSucceeded)
        {
            TempData["ActionMessage"] = "Comment successfully added. Comments need approval before being posted.";
            return RedirectToAction("show", "blog", new { id = comment.Blog.Id });
        }
        else
        {
            ModelState.AddModelError(string.Empty, errorMessage);
        }

        return RedirectToAction("show", "blog", new { id = comment.Blog.Id });

My Comment View Model is setup like this.

public class CommentListModel
{
    public IEnumerable<Comment> Comments { get; set; }
    public string ActionMessage { get; set; }
}

And my Blog View Model is setup like this.

public class BlogModel : ResultsModel<Blog>
{
    public Blog Blog { get; set; }
    public string ActionMessage { get; set; }

    public BlogModel() : base()
    {
        this.ItemsPerPageOptions = new KeyValuePair<int, string>[]
        {
            new KeyValuePair<int, string>(15, "Items Per Page: 15"),
            new KeyValuePair<int, string>(20, "Items Per Page: 20"),
            new KeyValuePair<int, string>(25, "Items Per Page: 25"),
            new KeyValuePair<int, string>(30, "Items Per Page: 30"),
            new KeyValuePair<int, string>(40, "Items Per Page: 40"),
            new KeyValuePair<int, string>(50, "Items Per Page: 50")
        };
    }
}

And the portion of the View that handles this.

   @if (TempData["ActionMessage"] != null)
        {   
            <div class="validation-summary-success" style="font-size: 2.1em; color: gray; background-color: #90EE90; padding: 10px; margin:2px;">
                <ul>
                    <li>@TempData["ActionMessage"]</li>
                </ul>
            </div>

            <script type="text/javascript">
                $(".validation-summary-success").delay(3000).fadeOut("slow").slideUp("slow");
            </script>   
        }

        <div class="Head">
          <h5>@Model.Blog.Title</h5>
          <label>By: @Model.Blog.Admin.Name <span>|  @Model.Blog.PostingDate.ToShortDateString()</span></label>
        </div>
        <article class="mContent">
            <p>@Model.Blog.Body</p>   
        </article>
        <div id="commentContainer">
                @Html.Partial("_BlogComments", @Model.Blog.Comments, new ViewDataDictionary { {"blogId", @Model.Blog.Id}  })
        </div>

As you can see, I’m using the TempDataDictionary to show a “Saved” message. However, it is my understanding that this defeats the purpose of having a strongly typed view.

When I save this comment in the Save action, how do I set the ActionMessage property if I am redirecting back to the Show action of the Blog controller if this action currently accepts a record ID? Would I set the ActionMessage property of the Blog or Comment object?

  • 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-06-11T09:25:46+00:00Added an answer on June 11, 2026 at 9:25 am

    I would use an Ajax form to post the new comment and get the status message. There’s no need to re-send the whole page for a single comment update, right? It looks like you could even return the status message as text, and update the status field using UpdateTargetId.

    @using Ajax.BeginForm("Save", "Comment", new AjaxOptions() {
        HttpMethod = "POST",
        UpdateTargetId = "StatusContainer",
        OnSuccess = "FadeOutStatus",
    }) {
        <input type='submit' value='Add Comment' />
    }
    
    <div class="validation-summary-success" style="display: none; font-size: 2.1em; color: gray; background-color: #90EE90; padding: 10px; margin:2px;">
        <ul>
            <li><span id="StatusMessage"></span></li>
        </ul>
    </div>
    
    <script type="text/javascript">
        $(".validation-summary-success").fadeIn().delay(3000).fadeOut("slow").slideUp("slow");
    </script>   
    

    (Above is untested, meant only for illustration.)

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

Sidebar

Related Questions

So I have a Blog object which has a list of tag objects (
I have a blog that has a redirect loop, and I can't understand htaccess
I have an application that manages documents called Notes. Like a blog, Notes can
I have a form which contains a whole heap of data entry fields that
I have a blog installed in www.foo.com/wp/ and would like all requests that go
In a blog application (which I have mostly built following a tutorial), I would
I found a blog entry which suggests that sometimes c# compiler may decide to
Let's say I have a business object that is very expensive to instantiate, and
I have an asp.net page for a simple blog which looks like <asp:TextBox ID=txtContent
I have a blog I wrote in PHP, and need to shorten the latest

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.