在我的應用程序中,我使用簡單的查看驗證,客戶端和服務器端驗證,工作正常,但現在我已經改變爲引導模態和PartialView。
問題是,客戶端驗證dos不再工作,並且對於服務器端驗證,當我單擊提交時,他將我重定向到新頁面(請參閱圖片),而不是在當前模式彈出窗口中顯示錯誤。 ASP.net MVC 4驗證在引導模式和部分視圖
Create Controller
:
[HttpGet]
public ActionResult Create()
{
ViewBag.CAT_ID = new SelectList(db.CATEGORIE, "CAT_ID", "LIBELLE");
ViewBag.C_GARANT = new SelectList(db.GARANTIE, "C_GARANT", "LIB_ABREGE");
return PartialView("_Create");
}
//
// POST: /Taux/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(TAUX taux)
{
if (ModelState.IsValid)
{
db.TAUX.Add(taux);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CAT_ID = new SelectList(db.CATEGORIE, "CAT_ID", "LIBELLE", taux.CAT_ID);
ViewBag.C_GARANT = new SelectList(db.GARANTIE, "C_GARANT", "LIB_ABREGE", taux.C_GARANT);
return PartialView("_Create", taux);
}
_Create
管窺:
@model pfebs0.Models.TAUX
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="CreateTaux">Add</h3>
</div>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="modal-body">
<div class="form-group">
<div class="form-group">
<label for="Categorie">Categorie : </label>
@Html.DropDownList("CAT_ID", String.Empty)
@Html.ValidationMessageFor(model => model.CAT_ID)
</div>
//Other Form input.
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default">Submit</button>
</div>
}
</div>
<script>
$("select").addClass("form-control");
$("input").addClass("form-control");
$("label").addClass("control-label");
</script>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval") }
Index View
,我puted模態:
<p>
@Html.ActionLink("Ajouter", "Create", "Taux",
new { id = "btnAdd", @class="btn btn-default"})
</p
<div id="modalDiv" class="modal fade" >
<div class="modal-dialog">
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function() {
$.ajaxSetup({ cache: false });
$('#btnAdd').click(function() {
$('.modal-dialog').load(this.href, function() {
$('#modalDiv').modal({
backdrop: 'static',
keyboard: true
}, 'show');
});
return false;
});
});
</script> }
所以我必須添加或陳ge在我的模式中進行客戶端驗證,並將服務器驗證重定向到模式,而不是像圖片一樣的新頁面?
我認爲你需要重新解析驗證器爲你的新的HTML元素dinamically加載:http://stackoverflow.com/questions/6691958/unobtrusive-jquery-validation-on-elements-created-by-javascript- mvc3另外,您可能需要將'Html.BeginForm'設置爲'Html.BeginForm(action,controller,method,null)',以避免奇怪的查詢字符串。 –
@ALMMa感謝您的驗證我解決了這個問題,但鋼鐵重定向問題如何解決它? – Chlebta
@Chlebta你有沒有得到這個解決方案,它不會將您重定向到新頁面,並實際顯示服務器端驗證?我有同樣的問題。 –