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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:38:45+00:00 2026-06-01T23:38:45+00:00

Very simple situation, here is my model: public class Comment { [Required] public string

  • 0

Very simple situation, here is my model:

public class Comment
{
    [Required]
    public string Name { get; set; }
    [Required]
    public string Text { get; set; }

}

Here is my controller:

public class CommentController : Controller
{
    //
    // GET: /Comment/Create

    public ActionResult Create()
    {
        return View();
    } 

    //
    // POST: /Comment/Create

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        Comment new_comment = new Comment();

        if (TryUpdateModel(new_comment))
        {
            return View(new Comment()); //problem is there
        }
        else
        {
            return View(new_comment);
        }

    }



}

And here is my standart genered strongly-typed View:

@model test.Models.Comment

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Comment</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Text)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Text)
            @Html.ValidationMessageFor(model => model.Text)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Problem is: when I enter valid data, TryUpdateModel() returns true, and

return View(new Comment());

should display empty form, because new empty instance of Comment is passed, but it still displays form with values entered earlier.

Please somebody tell me why. How to make it display empty form again?

  • 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-01T23:38:46+00:00Added an answer on June 1, 2026 at 11:38 pm

    Clear the modelstate:

    if (TryUpdateModel(new_comment))
    {
        ModelState.Clear();
        return View(new Comment()); //no more problem here
    }
    

    All HTML helpers first look at the ModelState when rendering their values and only after that in the model.


    Or even better and more properly use the Redirect-After-Post pattern (i.e. redirect after a successful POST):

    if (TryUpdateModel(new_comment))
    {
        return RedirectToAction("Create");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my situation; I've built a very simple web app that looks up a
The situation is very simple, I have two panel. In the event of OnMouseOver
Supose we have a very simple model: Station has at least one Train Train
Very simple example: #include <string> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc,
got a (I guess...) very simple problem: I want to set a session within
Having the following very simple xaml: <DocumentViewer Name=dv> <FixedDocument Name=fd Loaded=fd_loaded> <FixedDocument.Resources> <Style x:Key=TestStyle>
This seems like a very simple problem, but I cannot get a scrollbox to
Okay so here is my situation. About a year ago I released my very
This should be relatively simple, here is my situation; I have copied the trunk
Very simple question, I made the following program : #include <stdlib.h> int main(int argc,

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.