The problem:
I want to preform submit to form that is in modal and if validation faild to get the error message on the modal.
I'm using ajax validation (jQuery) as detailed here
Is there an elegant way to perform submit but on faild stay at modal to show error message?
My code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Cam c)
{
ViewBag.id = c.id;
using (Entities db = new Entities())
{
if (ModelState.IsValid)
{
db.camp.Add(c);
db.SaveChanges();
return RedirectToAction("Index", new { id = c.id });
}
}
return null;
}
Client:
@using (Html.BeginForm("Create", "Camp", FormMethod.Post, new { model =
Model }))
{
@Html.AntiForgeryToken()
<dt>
name:
</dt>
<dd>
@Html.TextBoxFor(model => model.name, new { @class = "form-control", @placeholder = "name", @id = "txtVenueID", style = "width:150px" })
</dd>
<dd>
@Html.ValidationMessageFor(model => model.name)
</dd>
<div class="modal-footer ">
<button type="button" class="btn btn-default" data-
dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
}
Model:
public partial class Cam
{
[Display(Name = "Name")]
[Required(ErrorMessage = "Require {0}")]
string name { get; set; }
}