【问题标题】:Close Popup Windows MVC Controller关闭弹出窗口 MVC 控制器
【发布时间】:2013-05-14 19:00:24
【问题描述】:

单击屏幕上的链接时会打开一个弹出窗口,包含该链接的视图是_DetailsS​​urvey。单击链接后,弹出窗口将打开,其中包含 _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


【解决方案1】:

打开一个弹出窗口是一个非常糟糕的主意,原因有很多,我不会在这里解释。我花了很长时间才找到一个我认为(对我来说)完美的解决方案。

就我而言,我将视图准备为部分视图(没有 html、标题和正文)。只有必要的项目才能在 div 中创建我的功能。

然后我使用 ajax 查询请求我的部分视图,之后我用我的部分视图字符串提供了一个 div,我将 JQuery 对话框方法应用于 div,以便我的视图显示为一个浮动对话框,我绑定了 OK ajax 帖子的按钮以将数据发送到服务器。 如果您想让不显眼的验证正常工作,您必须使用验证器解析表单。

我邀请你看看我的框架,你需要的一切都可用。

https://myprettycms.codeplex.com/SourceControl/latest#461106

【讨论】:

  • 我似乎无法找到您在哪个组件中实现我的问题,您在哪个控制器中绑定了验证,您能给我解释一下吗?谢谢
猜你喜欢
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
相关资源
最近更新 更多