0

Hi i want to clear all fields once the submit button is clicked in MVC5.I used ModelState.clear()in controller. But this only clear the textbox value. its not clearing the Radio button and dropdown values. So any one tell me the solution for this issue.

My ViewModel(CustomerViewModel)

    public System.Guid CustomerID { get; set; }
    [Required(ErrorMessage = " Enter The Customer Name.")]
    [StringLength(100, ErrorMessage = "Name Only Accepted 100 Characters.")]
    public string CustomerName { get; set; }
    public System.Guid CustomerTypeID { get; set; }
    [Required(ErrorMessage = "CustomerType Required.")]
    public string CustomerType { get; set; 
    public System.Guid AddressID { get; set; }
    public string Address { get; set; }
    public string Street { get; set; }
    public string Location { get; set; }
    public string Place { get; set; }
    public Nullable<System.Guid> AreaID { get; set; }
    public string Area { get; set; }
    public Nullable<System.Guid> CityID{ get; set; }
    public string  City { get; set; }
    public string PinCode { get; set; }

My View

  @Html.Label("Customer Name", new { @class = "control-label" })
  @Html.ValidationMessageFor(model => model.CustomerName)
  @Html.TextBoxFor(model => model.CustomerName, new { @class = "form-control required", type = "text" })


   @Html.Label("Customer Type", new { @class = "control-label" })
   @Html.DropDownList("CustomerTypeID", null, "Select", new { @class = "form-control required" })


   @Html.LabelFor(model => model.Street, new { @class = "control-label" })
   @Html.TextBoxFor(model => model.Street, new { @class = "form-control", type = "text required" })
   @Html.ValidationMessageFor(model => model.Street)


   @Html.LabelFor(model => model.Location, new { @class = "control-label" })
   @Html.TextBoxFor(model => model.Location, new { @class = "form-control", type = "text " })
   @Html.ValidationMessageFor(model => model.Location)


    @Html.LabelFor(model => model.Place, new { @class = "control-label" })
    @Html.TextBoxFor(model => model.Place, new { @class = "form-control", type = "text" })
    @Html.ValidationMessageFor(model => model.Place)


    @Html.LabelFor(model => model.Area, new { @class = "control-label" })
    @Html.DropDownList("AreaID", null, "Select", new { @class = "form-control required" })

    @Html.LabelFor(model => model.PinCode, new { @class = "control-label" })
    @Html.TextBoxFor(model => model.PinCode, new { @class = "form-control", type = "text" })
    @Html.ValidationMessageFor(model => model.PinCode)

My Controller

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

   [HttpPost]

   Public ActionResult Create(CustomerViewMode CVM)
   {

   var Customerobj = new Customer
   {
    CustomerID= Guid.NewGuid(),
    CustomerName= CVM.CustomerName;
    // remaining saving code

   };

    db.Customers.Add(Customerobj);
     ModelState.Clear();
        return View();
    }

Advance Thanks..

Priya
  • 23
  • 1
  • 10

1 Answers1

0

ModelState.clear() is used to clear errors in ModelState dictionary or any validations set by the user. You can use following code,where in POST method redirection is done to GET method and new model is initialized.

 [HttpGet]
public ActionResult Index()
{
    AdminModel model = new AdminModel();
    return View(model);
}
[HttpPost]
public ActionResult Index(AdminModel model,FormCollection c)
{
    if (ModelState.IsValid)
    {
       return RedirectToAction("Index", new { Controller = "Sample1" });
    }

        return View(model);

}
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
  • No i dont want to return Index page. I have to put many entries . so i cant able to click every time add button and enter the form. So only im asking this clear option.. – Priya Aug 01 '16 at 04:24