• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

MVC Post question

In the controller...

        [HttpPost]
        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)
        </div>

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?

Thanks!
0
gswitz
Asked:
gswitz
  • 4
2 Solutions
 
gswitzAuthor Commented:
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?
0
 
gopaltaydeCommented:
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");
0
 
gswitzAuthor Commented:
Actually, I stepped through it and it returns true and executes the line

ctnt.Name="Success";

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.
0
 
gswitzAuthor Commented:
Using

ModelState.Clear();

Solves the problem.
0
 
gswitzAuthor Commented:
My answer is the right answer.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now