【问题标题】:Modal window create from Partial View (ASP.NET MVC)从局部视图(ASP.NET MVC)创建模态窗口
【发布时间】:2017-08-27 12:42:58
【问题描述】:

我有查看我显示电子邮件列表的位置

我需要使用模态窗口和部分视图在表中创建新记录。

这是部分视图的代码

    @model SmartSolutions.Models.Question

<div>
    <div class="form-group" style="text-align:center;padding-bottom: 40px; padding-top: 30px;">
         @Html.TextAreaFor(m => m.question, new { @class = "form-control", placeholder = "Вопрос", id = "question" })
    </div>
    <div class="form-group" style="text-align:center;padding-bottom: 40px; padding-top: 30px;">
        @Html.TextAreaFor(m => m.TimeForAnswer, new { @class = "form-control", placeholder = "Время на ответ", id = "answer" })
    </div>
    <div class="form-group" style="text-align:center;padding-bottom: 40px; padding-top: 30px;">
        @Html.TextAreaFor(m => m.TimeForReady, new { @class = "form-control", placeholder = "Время на подготовку" , id = "prepare" })
    </div>
    <div class="form-group" style="text-align:center;padding-bottom: 40px; padding-top: 30px;">
        @Html.TextAreaFor(m => m.Retries, new { @class = "form-control", placeholder = "Попытки", id = "retries" })
    </div>
    <div class="form-group" style="text-align:center">
        <input type="button" id="save" value="Создать" class="btn btn-default" style="margin-right: 40px;" />
    </div>
</div>
<script>
       $(document).ready(function () {
        $('#save').click(function () {
            save();
        });
    });
        function save() {
            $.ajax({
                type: 'Post',
                dataType: 'Json',
                data: {
                    Question_new: $('#question').val(),
                    Answer: $('#answer').val(),
                    Preparing: $('#prepare').val(),
                    Retries: $('#retries').val(),
                      },
                url: '@Url.Action("WelcomeWriter", "Interwier")',
                success: function (da) {
                    if (da.Result === "Success") {

                        window.location.href = da.RedirectUrl;

                    } else {

                        alert('Error' + da.Message);
                    }
                },
                error: function (da) {
                    alert('Error');
                }
            });
        }
</script>

我在局部视图中进行局部视图和 AJAX 调用

这是post方法的代码

  public ActionResult CreateNewQuestion( string Question_new, string Answer, string Preparing, string Retries)
    {
        Question quest = new Question
        {

            question = Question_new,
            TimeForAnswer = Answer,
            TimeForReady = Preparing,
            Retries = Retries,
        };
        db.Questions.Add(quest);
        db.SaveChanges();

        return Json(new { Result = "Success", Message = "Saved Successfully"});

    }

我有查看按钮单击的位置,我需要在其中显示带有部分视图的模式。当我在模态中单击#save 按钮时,它将关闭。

这是它现在的样子(只是进入新视图)

 <div style="height: 20%;">
            <button class="btn btn-default" style="margin-top: 15px; margin-left: 20px;">
                @Html.ActionLink("Добавить вопрос", "Create", "Questions", null, new { @style = "color:white;" })
            </button>
            <button class="btn btn-default" style="margin-top: 15px; margin-left: 200px;">
                @Html.ActionLink("Далее", "RoleForSending", "Questions", null, new { @style = "color:white;" })
            </button>
        </div>

我怎么能意识到这一点?

【问题讨论】:

  • 嗨,Eugene,你在使用 jQuery、bootstrap、EasyUI 等 JavaScript 框架吗?
  • 我在 @GlaucoCucchiar 项目中安装了 Bootstrap 和 jQuery

标签: c# asp.net ajax asp.net-mvc asp.net-mvc-4


【解决方案1】:

您可以使用 jQuery UI/bootstrap 来显示您的模式。

我会用 jQuery UI 来做这个:

1 - 在主视图中插入 partial_view

<div id="containerForPartialView">
    @Html.Partial("~/<pathOfYourPartial>", <yourModel>)
</div>

2 - 在 javascript 中初始化您的模式(在您的主视图中)

var myFormDialog = $("containerForPartialView").dialog(
   {autoOpen : false, modal : true});

3 - 让您的按钮变得简单

<button id="myButtonForShowDialog">Create new question</button>

4 - 在您的按钮上附加一个事件以显示您的对话框(我的意思是您的主视图中的“按钮”标签):

$("myButtonForShowDialog").button().on("click", function() {
    myFormDialog.dialog("open");
});

完整的 jQuery UI 参考请看这里:jQuery UI Modal dialog
有关 boostrap 模态的参考,请看这里:Bootstrap modal

【讨论】:

  • 看来我无法在@Html.Partial 中编写模型我有这个@model IEnumerable&lt;SmartSolutions.Models.Question&gt; 并像这样写&lt;div id="containerForPartialView"&gt; @Html.Partial("~/Views/Questions/CreateQuestion.cshtml",Model) &lt;/div&gt; 并且有错误The model item passed into the dictionary is of type 'System.Collections.Generic.List1[SmartSolutions.Models.Question]',但是此词典需要“SmartSolutions.Models.Question”类型的模型项。`
  • 是的,您的模型在主视图中是 IEnumerable of Question,但部分视图只接受一个问题。如果您的模态用于创建,您可以使用“new Question()”作为模型或不使用模型:'@Html.Partial("~/Views/Questions/CreateQuestion.cshtml")'
  • 嗯,我写的没有模型,我又出现了这个错误。也许是因为我在部分视图中有模型?
  • 不,你可以有一个部分模型,你可以在不传递模型的情况下渲染部分。您确定正确编辑并重新编译了吗?
  • 现在将尝试重建。
猜你喜欢
  • 2013-04-07
  • 2018-07-05
  • 2010-12-06
  • 2021-02-09
  • 1970-01-01
  • 2016-06-09
  • 2011-06-28
  • 1970-01-01
  • 2011-09-11
相关资源
最近更新 更多