我正在使用MVC局部视图显示Jquery模型弹出窗体。在提交时,我调用我的动作方法来验证用户输入,如果验证失败,我需要再次填充模态弹出框。我面临的问题是,在提交时它调用子动作方法,我能够在我的部分视图中获得ViewData.ModelState.IsValid,但是不能根据ModelState值自动显示模式弹出。我尝试下面的jquery代码,但没有运气。
<<p> 部分视图/strong>@inherits Umbraco.Web.Mvc.UmbracoViewPage<WebApplicationDemo1.Models.JobAlertModel>
<script>
$(function () {
$("#dialog-modal").dialog({
autoOpen: false,
width: 600,
height: 550,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "dissolve",
duration: 1000
}
});
$("#modal-opener").click(function () {
$("#dialog-modal").dialog("open");
});
});
</script>
<button id="modal-opener">Job Alerts</button>
<div id="dialog-modal" title="Basic modal dialog">
@using (Html.BeginForm())
{
<fieldset>
<legend>Product</legend>
<div class="editor-label">
@Html.LabelFor(m => m.email )
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.email)
@Html.ValidationMessageFor(model => model.email)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.location)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.location)
@Html.ValidationMessageFor(model => model.location)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.password)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.password)
@Html.ValidationMessageFor(model => model.password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.search)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.search)
@Html.ValidationMessageFor(model => model.search)
</div>
<input type="submit" value="Create new job alert" />
</fieldset>
}
</div>
@if (!ViewData.ModelState.IsValid)
{
<div>There are some errors</div>
<script type="text/javascript">
$("#modal-opener").click();
</script>
}
<<p> 孩子行动/strong> [ChildActionOnly]
[HttpPost]
public ActionResult JobAlert(JobAlertModel JobAlert)
{
JobAlertModel ja = new JobAlertModel();
if (ModelState.IsValid)
{
ja.email = JobAlert.email;
return Redirect("/");
}
else
{
return PartialView("popup", ja);
}
}
我自己想。如果有人想知道的话。
@if (!ViewData.ModelState.IsValid)
{
<script type="text/javascript">
$(document).ready(function () {
$("#modal-opener").click();
});
</script>
}