asp.net MVC

How to clear the fields after submitting a form in asp.net MVC?

How to clear the fields after submitting a form in asp.net MVC?, someone asked me to explain?

button onclick submit form

 

In this article, I will show you how to clear form after submit in asp.net MVC. Before, when I submitting the asp.net mvc form; the page fields are not cleared; the problem on resetting of all form fields. But the page still have the values.

This is due to HTML helpers, It will completely ignore the model class. So that we need to clear the class and return view. After this, It worked perfectly.

Here, I used contact class.

 

Contact class: 

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

        [
Required, EmailAddress]
        public string Email { get; set; }

        [
Required]
        public string Subject { get; set; }

        [Required]
       public string Body { get; set; }
    } 

 

ContactUsController:

 [HttpPost]
       public ActionResult Save(Contact contact)
        {
            //Save data to DB here ...         

            ModelState.Clear();
            Contact ObjContact = new Contact() { Name = string.Empty, Email = string.Empty, Subject = string.Empty, Body = string.Empty };
            return View("Index", ObjContact);

       }

 

Post your comments / questions