how to clear textboxes in mvc5

Posted by Klbaiju under ASP.NET MVC on 3/27/2017 | Points: 10 | Views : 218 | Status : [Member] | Replies : 1
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




Responses

Posted by: A2H on: 3/27/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can try with the below approach
Provide an id to your cancel button
<input type="button" name="cancel" id="btncancel" value="cancel" class="btn btn-default" />

Below code will call on button click and clear all textboxes in form
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("#btncancel").click(function () {
alert('sdfsdf')
$('input:text').val('');
return false;
});
});


</script>


Thanks,
A2H
My Blog

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response