当我在屏幕上点击一个链接时,会弹出一个窗口,该链接所在的视图名为_DetailsSurvey。在点击链接后,弹出窗口会显示_EditSurvey视图的内容。在_EditSurvey视图的末尾,我有一个按钮。
<input type="submit" value="Save" />
我正在使用Ajax选项,如果模型状态有效,则在按钮点击后将一行插入到Survey表中。
@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>
如果提供的输入有任何问题,我会在每个字段旁边显示验证消息。如果模型有效,我想要关闭弹出窗口。