MVC Post question

Posted on 2012-09-11
Last Modified: 2012-09-17
In the controller...

        public ActionResult CreateContinent(string Name, string Area, string Highpoint,
           string Height, string Lowpoint, string Depth)

            Mvc3Application1.Models.Continent ctnt = new Mvc3Application1.Models.Continent();
            //Mvc3Application1.Models.Continent ctnt = c.GetContinents(sContinent);

            ctnt.Name = Name;
            ctnt.Area = Convert.ToInt64(Area);
            ctnt.Highpoint = Highpoint;
            ctnt.Height = Convert.ToInt64(Height);
            ctnt.Lowpoint = Lowpoint;
            ctnt.Depth = Convert.ToInt64(Depth);
            Mvc3Application1.Models.GetContinent d = new Mvc3Application1.Models.GetContinent();
            if (d.CreateContinent_data(ctnt) > 0)
                ctnt.Name = "Success";
            return View(ctnt);

In the view...
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)

The view is persisting the originally entered value not the value of Success even when the object property is set to success prior to passing the object back to the view.

Can anyone help?

Question by:gswitz

    Author Comment

    I recognize that my post might not have been clear enough.

    I have a view typed on a class (strongly typed). The view is for creating new items. On the httpget in the controller, I can new up the class the view is typed on, set some values and return it to the view, setting default values in the text boxes.

    The user can then set values in the text boxes and click a button to post back.

    The httppost method in the controller catches it with every value as an input parameter in the signature. I then Insert the data into the database.

    I want to clear the values returned to the view, so I new up a new instance of the class, set the values and return it to the view.

    The problem is this...
    The values displayed on the page are the same as what the user entered, not the cleared values I passed from the controller to the view.

    It seems like there might be a page is dirty or something? I don't know...
    It's strange that when I pass it from the httpget and step through the view, the values are set in the fields, but when I pass it from the post to the view, model.Name has a does not exist or cannot be found or something... I don't know why.

    Any thoughts?
    LVL 7

    Assisted Solution

    My guess is that the below condition is returning false

                if (d.CreateContinent_data(ctnt) > 0)
                    ctnt.Name = "Success";

    hence it is not updating then name. Try to debug the code or write the else part.

    But generally, we should return to index page after adding something by using -

    return RedirectToAction("Index");

    Author Comment

    Actually, I stepped through it and it returns true and executes the line


    So, I can't really explain it. It's like I need to set a page is dirty property or something so that it will repaint. I have no idea.

    Accepted Solution



    Solves the problem.

    Author Closing Comment

    My answer is the right answer.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now