1

So I have a simple action in my controller. The project is a MVC Mobile Application.

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

this gives an form to enter data. I then handle the data in the post back.

[HttpPost]
public ActionResult Index(ScanViewModel model)
{
    if (ModelState.IsValid)
    {
        Scan ns = new Scan();
        ns.Location = model.Location;
        ns.Quantity = model.Quantity;
        ns.ScanCode = model.ScanCode;
        ns.Scanner = User.Identity.Name;
        ns.ScanTime = DateTime.Now;

        _db.Scans.Add(ns);
        _db.SaveChanges();

    }

    return View(model);
}

I want to clear the fields in the form and allow users to enter data again. However I get the exact same values back into my inputs. How can I clear them in the controller.

2 Answers 2

1

Just call this.ModelState.Clear()

Sign up to request clarification or add additional context in comments.

Comments

1

You should follow the PRG pattern.

Just redirect to the Action method which is meant for the Create Screen. You can use the RedirectToAction method to do so.

RedirectToAction returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

[HttpPost]
public ActionResult Index(ScanViewModel model)
{
   if(ModelState.IsValid)
   {
       //Code for save here
       //..............
       _db.SaveChanges();
       return RedirectToAction("Index","User");
   }
   return View(model);
}
public ActionResult Index()
{
   return View();
}

Assuming your controller name is UserController.

1 Comment

A note on this - if you want to carry a message you will need to use TempData rather than ViewBag as it's a new request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.