c# - 关闭弹出窗口 MVC Controller

标签 c# javascript asp.net-mvc-3 asp.net-mvc-4

单击屏幕上的链接时,我打开了一个弹出窗口,具有该链接的 View 是 _DetailsS​​urvey。单击链接后,弹出窗口将打开,其中包含 _EditSurvey View 的内容。在 _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;
    });
});

在我的弹出 View 中,我使用的是之前显示的 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>

如果提供的输入有任何问题,我会在每个字段旁边显示验证消息。如果模型有效,我想关闭弹出窗口。

最佳答案

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

在我的例子中,我将 View 准备为局部 View (没有 html、标题和正文)。只有必要的项目才能在 div 中创建我的功能。

然后我使用 ajax 查询请求我的部分 View ,在我用我的部分 View 字符串提供一个 div 之后,我将 JQuery 对话框方法应用到 div 以使我的 View 显示为 float 对话框,然后我绑定(bind) OK按钮到 ajax post 以将数据发送到服务器。 如果您想让不显眼的验证生效,您必须使用验证器解析表单。

我邀请你看看我的框架,你需要的都有。

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

关于c# - 关闭弹出窗口 MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16645807/

相关文章:

c# - 关于C#数据类型的问题

javascript - AngularJS ng-repeat 不重复,没有控制台错误

javascript - 通过 javascript 按钮传递多个元素更改

asp.net-mvc - 为什么我在 MVC3 Controller 内的模型中更新的值未呈现在客户端上?

c# - C# WPF 代码隐藏中的按钮处理程序

c# - 我怎样才能避免错误地更新当前对象上的另一个对象?

c# - LINQ 生成不正确的 SQL(引用不存在的表)

php - 将用户的输入存储到数据库中

asp.net-mvc-3 - Ajax.ActionLink 不会停留在同一页面上

javascript - ASP.NET MVC3 - 每个选项卡中的 Controller 正在运行