You must Sign In to post a response.
  • Category: ASP.Net MVC

    Clear text boxes in mvc5

    Hi,

    I want to clear textboxes when presses cancel button

    here is my model

    namespace MvcMovie.Models
    {
    public class Movie
    {
    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string Genre { get; set; }
    public decimal Price { get; set; }
    }
    public class MovieDBContext : DbContext
    {
    public DbSet<Movie> Movies { get; set; }
    }
    }

    this is my view

    @model MvcMovie.Models.Movie

    @{
    ViewBag.Title = "Create";
    }

    <h2>Create</h2>

    @using (Html.BeginForm("Create", "Movies", FormMethod.Post))
    {
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
    <h4>Movie</h4>
    <hr />
    @Html.ValidationSummary(true)

    <div class="form-group">
    @Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.Title)
    @Html.ValidationMessageFor(model => model.Title)
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.ReleaseDate, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.ReleaseDate)
    @Html.ValidationMessageFor(model => model.ReleaseDate)
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.Genre, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.Genre)
    @Html.ValidationMessageFor(model => model.Genre)
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.Price)
    @Html.ValidationMessageFor(model => model.Price)
    </div>
    </div>

    <div class="form-group">
    <div class="col-md-offset-2 col-md-10">
    <input type="submit" value="Create" class="btn btn-default" />
    <input type="submit" name="cancel" value="cancel" class="btn btn-default" />
    </div>
    </div>
    </div>
    }

    <div>
    @Html.ActionLink("Back to List", "Index")
    </div>

    @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    }

    and finally this is my controller

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


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include="ID,Title,ReleaseDate,Genre,Price")] Movie movie,string cancel)
    {

    if (!string.IsNullOrEmpty(cancel))
    {



    ModelState.Clear();

    }
    else
    if (ModelState.IsValid)
    {

    db.Movies.Add(movie);
    db.SaveChanges();
    return RedirectToAction("Index");
    }


    return View(movie);
    }

    the above code is not working

    how to solve this

    Regards

    Baiju
  • #769133
    you should need to use: ModelState.Clear() . This is because when it return sthe View all previous entered data will be cleared. Example
    public ActionResult Index()
    {
    return View();
    }

    [HttpPost]
    public ActionResult Index(FormCollection collection)
    {
    // This will clear all values of textbox
    ModelState.Clear();

    return View();
    }


    Reference URL: http://stackoverflow.com/questions/4887192/clearing-text-box-fields-on-a-page-in-mvc


  • Sign In to post your comments