我在验证部分视图方面遇到了问题。我的索引视图是由少数部分视图制作的。正在使用其中一个视图_Contact,以便用户可以提交即将举办的活动。我想启用检查是否有人已经通过输入的电子邮件提交了事件。一切正常,但问题是它没有显示有关失败提交的消息(它在服务器端验证失败,但不会向用户显示错误)。这是我的代码:
Index:
...
@if(ViewBag.Competitor==null)
{@Html.Partial("_Contact", new CompetitorViewModel())}else
{
@Html.Partial("_Contact",(CompetitorViewModel)ViewBag.Competitor)
}
...
_Contact:
@model Models.CompetitorViewModel
<script src="@Url.Content("~/Scripts/jquery.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"> </script>
<section id="contact">
<div id="contact-us" class="parallax">
<div class="container">
<div class="row">
<div class="heading text-center col-sm-8 col-sm-offset-2 wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
</div>
</div>
<div class="contact-form wow fadeIn" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="row">
<div class="col-sm-6">
@using (Html.BeginForm("SubmitStudent", "Home", FormMethod.Post, new { id = "main-contact-form" }))
{
@Html.ValidationSummary(true)
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-sm-6">
<div class="form-group">
@Html.TextBoxFor(model=>model.firstName, new { @class = "form-control", @placeholder="Ime"})
@Html.ValidationMessageFor(model=>model.firstName)
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.TextBoxFor(model=>model.lastName, new { @class = "form-control", @placeholder="Prezime"})
@Html.ValidationMessageFor(model=>model.lastName)
</div>
</div>
</div>
<div class="form-group">
@Html.TextBoxFor(model=>model.eMail, new { @class = "form-control", @placeholder="Email Adresa"})
@Html.ValidationMessageFor(model=>model.eMail)
</div>
<div class="form-group">
@Html.DropDownListFor(m=> m.collegeId, (IEnumerable<SelectListItem>)ViewBag.Colleges)
</div>
<div class="form-group">
<input type="submit" value="Prijavi me" class="btn-submit" />
</div>
}
</div>
<div class="col-sm-6">
<div class="contact-info wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
</div>
</div>
</div>
</div>
</div>
</div>
</section><!--/#contact-->
main.js:
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
type:"POST",
url: '/Home/SubmitStudent',
data: $('#main-contact-form').serializeArray(),
cache: false,
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Your request is being sent...</p>').fadeIn() );
}
}).
success(function(data){
form_status.html('<p class="text-success">Thank you for submitting to this event</p>').delay(3000).fadeOut();
}).
complete(function ()
{
$("#main-contact-form").each(function () { $.data($(this)[0], 'validator', false); });
$.validator.unobtrusive.parse("#main-contact-form");
})
});
HomeController中:
[HttpPost]
public ActionResult SubmitStudent(CompetitorViewModel c)
{
var colleges = _db.colleges.Distinct().ToList();
ViewBag.Colleges = new SelectList(colleges, "id", "Name");
if (!ModelState.IsValid)
{
ViewBag.Competitor = c;
}
else
{
var competitors = _db.competitors;
foreach (Competitor c1 in competitors)
{
if (c1.eMail == c.eMail)
{
ModelState.AddModelError("c.eMail", "That E-mail is already registered to this event.");
ViewBag.Competitor = c;
return View("Index");
}
}
DCCollege college = (DCCollege)_db.colleges.Find(c.collegeId);
Competitor competitor = new Competitor();
competitor.firstName = c.firstName;
competitor.lastName = c.lastName;
competitor.eMail = c.eMail;
competitor.college = college;
competitors.Add(competitor);
_db.Save();
}
return View("Index");
}
如果有人能帮助我,我真的很感激。我已经挣扎了很长一段时间。
答案 0 :(得分:0)
我很快查看了代码。我认为您没有看到验证消息,因为您将其添加到ModelState,但是您没有将模型传递给视图。