单击屏幕上的链接时,我打开了一个弹出窗口,具有该链接的视图是 _DetailsSurvey。单击链接后,弹出窗口将打开,其中包含 _EditSurvey 视图的内容。在 _EditSurvey 结束时,我有按钮
<input type="submit" value="Save" />
我正在使用 Ajax 选项,单击按钮后,如果模型状态有效,我会在调查表中插入一行。
@using (Ajax.BeginForm("SubmitSurvey", "Blog", null, new AjaxOptions
{
UpdateTargetId = "context",
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
HttpMethod = "Post",
Url = "/Home/SubmitSurvey"
},
new { surveyItem = @Model }))
我想要做的是如果从 SubmitSurvey 方法返回后模型状态有效,我希望关闭弹出窗口。我使用以下方法来实现这一点,但它不起作用。
Employee employee;
if (ModelState.IsValid)
{
int employeeId = surveyItem.EmployeeId;
int trainingId = surveyItem.TrainingId;
employee = _work.EmployeeRepository.GetSet().
FirstOrDefault(a => a.Id == employeeId);
var training = _work.TrainingRepository.GetSet().Where(a => a.EmployeeId == employeeId && a.Id == trainingId).ToList().ElementAt(0);
training.Survey = surveyItem.survey;
training.SurveyId = surveyItem.survey.Id;
/* _work.SurveyRepository.Add(surveyItem.survey);
_work.SurveyRepository.Save();*/
_work.TrainingRepository.UpdateAndSave(training);
_work.TrainingRepository.Save();
}
else
{
return PartialView("_EditSurvey", surveyItem);
}
return JavaScript("window.close()");
我创建我的弹出链接如下
<tr>
<td class="view_detail_label">
Eğitim Adı
</td>
<td>
@Html.ActionLink(
training.Name.Name,
"AddSurvey",
new {
employeeId = Model.Id,
trainingId = training.Id
},
new {
@class = "addSurvey"
}
)
<div class="result" style="display:none;"></div>
</td>
</tr>
调用的ajax代码如下:
$(document).ready(function () {
$('.addSurvey').click(function () {
$.ajax({
url: this.href,
type: 'GET',
cache: false,
context: this,
success: function (result) {
$(this).next('.result').html(result).dialog({
autoOpen: true,
title: 'Anket',
width: 500,
height: 'auto',
modal: true
});
}
});
return false;
});
});
在我的弹出视图中,我使用了之前显示的 Ajax BeginForm,在它下面我有一个表格,用户输入调查的值。最后我有提交按钮。
<table id="context">
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.survey.Context)
</div>
</td>
<td>
<div class="editor-field">
@Html.EditorFor(model => model.survey.Context)
@Html.ValidationMessageFor(model => model.survey.Context)
</div>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Kaydet" />
</td>
</tr>
如果提供的输入有任何问题,我会在每个字段旁边显示验证消息。如果模型有效,我想关闭弹出窗口。