i want to make a form validation with the ajax language. Just a simple one so i can start expanding it. I have a model, controller and a view, but i don't know how to start go ahead from here.
<form id="myForm">
@Html.TextBoxFor(model =>model.News, new { @class = "form-control", @placeholder = "Email" })
<input type="button" value="SignUp" class="btn-block btn-primary" id="btnSubmit"/>
</form>
<script>
$(document).ready(function () {
$("#btnSubmit").click(function () {
debugger
var data = $("#myForm").serialize();
$.ajax({
type: "POST",
url: "/News/Index",
data: data,
success: function (response) {
alert("Data saved to the database");
}
})
})
})
</script>
Model :
public class News
{
public int NewsId{ get; set; }
public string Email { get; set; }
}
Controller:
[HttpPost]
public ActionResult Index(News model)
{
try
{
News newsletter= new News();
newsletter.Email = model.Email;
_context.News.Add(newsletter);
_context.SaveChanges();
}
catch (Exception e)
{
throw e;
}
return View(model);
}