【发布时间】:2013-05-14 19:00:24
【问题描述】:
单击屏幕上的链接时会打开一个弹出窗口,包含该链接的视图是_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>
如果提供的输入有任何问题,我会在每个字段旁边显示验证消息。如果模型有效,我想关闭弹出窗口。
【问题讨论】:
-
从 MVC 控制器返回的Javascript?你能做到吗?你为什么要这样做?您没有向费心填写您给他们的表格的用户提供任何反馈。使用 jQuery 对话框;使用您的表单加载部分视图。使用不显眼的验证在客户端上验证,并在提交后再次验证!然后返回一个局部视图,通知用户他们的表单已保存。让他们关闭对话框。
-
您可以返回 Javascript,还有一些其他相关的主题,但在这种情况下它不起作用。当模型无效时,我会通过模型中每个字段的验证消息向用户提供反馈。我怎样才能在视图中成功完成操作?
-
我认为这有点反模式,但无论如何;试试这个stackoverflow.com/questions/7271005/…
-
这也不起作用,当我从验证模型返回并尝试关闭窗口时,我已经将 ViewBag.close 设置为 true,但它不起作用。提供的链接也不起作用
标签: c# javascript asp.net-mvc-3 asp.net-mvc-4