简体   繁体   English

如何在ASP.NET MVC的弹出窗口中创建一个空表单?

[英]How do I create an empty form inside a popup in asp.net mvc?

I have a built a form in asp.net mvc (the left side is what I've built, the right side is what I'd like to add): 我在asp.net mvc中建立了一个表单(左侧是我构建的表单,右侧是我要添加的表单):

发送测试电子邮件概念

I'd like the Send Test Email button to launch a popup window that has a blank form. 我希望“发送测试电子邮件”按钮启动一个具有空白表格的弹出窗口。 One textbox to enter the recipient's email, another to enter the sender's email, and a button to send the email. 一个文本框用于输入收件人的电子邮件,另一个文本框用于输入发件人的电子邮件,以及一个用于发送电子邮件的按钮。 Now this popup needs to gather a few fields from the main form that launched it: Subject and Body (body not shown in the screenshot). 现在,此弹出式窗口需要从启动它的主要表单中收集一些字段:Subject和Body(屏幕快照中未显示的body)。 How can I do this? 我怎样才能做到这一点? I won't know the two email addresses ahead of time, so they wouldn't already exist in a model object. 我不会提前知道这两个电子邮件地址,因此它们在模型对象中将不存在。 Here's some of my code so far (in a view called EmailTemplate.cshtml): 到目前为止,这是我的一些代码(在名为EmailTemplate.cshtml的视图中):

<div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" /> &nbsp;&nbsp;                    
                <button id="btnSendTestEmail">Send Test Email</button>

                <div id="SendTestEmail"></div>

            </div>
        </div>

That's for the initial button to launch the popup and for the area to place it on the page. 这是用于启动弹出窗口的初始按钮,以及用于将其放置在页面上的区域。 And here's the javascript I'm starting to write, but am not sure how to finish: 这是我将开始编写的javascript,但不确定如何完成:

var url = '@Url.Action("SomeActionHere","SomeControllerHere")';

    $('#btnSendTestEmail').click(function () {
        $("#SendTestEmail").load(url);
    });

It seems like I have to create an action method, but I don't know why, because I don't need to prepopulate the two email textboxes. 似乎我必须创建一个操作方法,但是我不知道为什么,因为我不需要预先填充两个电子邮件文本框。 And I also seem to have to specify a controller, but I don't know if that controller is meant to prepopulate the form or handle the form when it's submitted (when the send test email button on the popup is clicked) I've created a partial view called EmailTestForm.cshtml: 而且我似乎还必须指定一个控制器,但是我不知道该控制器是用于填充表单还是在提交表单时(单击弹出窗口中的发送测试电子邮件按钮时)处理表单。称为EmailTestForm.cshtml的局部视图:

@model EmailTemplateEditor.Models.TestEmail

<form id="SendTestEmail">

    Recipient Email Address: @Html.TextBox("RecipientEmail")
    Sender Email Address: @Html.TextBox("SenderEmail")
    <br/>
    <input type="submit" value="Send Test Email" class="btn btn-default" /> 

</form>

And I even created a model for this, though I'm not sure I need to (I'm new to mvc and it still feels like I'm often shoehorned into making lots and lots of models) 而且我什至为此创建了一个模型,尽管我不确定我是否需要(我是mvc的新手,但仍然觉得我经常被弯腰制造很多模型)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EmailTemplateEditor.Models
{
    public class TestEmail
    {        
        public string RecipientEmail { get; set; }

        public string SenderEmail { get; set; }
    }
}

Ultimately, when the popup's send test email button is clicked, I want to grab the 2 email addresses, the body and subject from the main form, pass them into an method where I can pass them into a stored procedure call. 最终,当单击弹出窗口的“发送测试电子邮件”按钮时,我想从主表单中获取2个电子邮件地址(正文和主题),将它们传递给方法,然后将它们传递给存储过程调用。 Am I even approaching this the right way here, or am I way off? 我是否在这里正以正确的方式走,还是要离开? I've found a few SO posts that are somewhat like this case, but different enough for me to still feel lost. 我发现了一些类似这种情况的SO帖子,但它们的区别足以让我仍然迷失方向。

I figured it out. 我想到了。 I had to make several changes: 我必须进行一些更改:

  1. Move my modal div to outside of containing page's form 将我的模式div移到包含页面形式的外部
  2. Display the modal div when a button was clicked (per Stephen Muecke's comment) 单击按钮时显示模式div(根据Stephen Muecke的评论)
  3. Move the submit javascript code for when the test email button is clicked from the containing page into the partial view page. 将包含单击测试电子邮件按钮时的提交javascript代码从包含的页面移到部分视图页面。
  4. Add onclick="SubmitAction()" to the submit button 将onclick =“ SubmitAction()”添加到提交按钮

Here's my final code (shortened): 这是我的最终代码(简化):

EmailTemplate.cshtml (the containing page) EmailTemplate.cshtml(包含页面)

@model EmailTemplateEditor.Models.EmailTemplate

@{
    ViewBag.Title = "Email Template";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Email Template</h2>

@using (Html.BeginForm("Edit", "EmailTemplate", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">        
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.ID)

            <div class="form-group">
                @Html.LabelFor(model => model.Subject, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Subject)
                    @Html.ValidationMessageFor(model => model.Subject)
                </div>
            </div>                        

            <div class="form-group" tabindex="-1">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" /> &nbsp;&nbsp;
                    <a id="btnSendTestEmail">Send Test Email</a>

                    <div id="SendTestEmail"></div>

                </div>
            </div> 
    </div>
}

<div class="modal" id="SendTestEmailModal">
    <div class="modal-content">
        @Html.Partial("EmailTestForm", new EmailTemplateEditor.Models.TestEmail(Model.Body, Model.Subject))
        <span class="close">&times;</span>
    </div>
</div>

<script>

    $(document).ready(function () {                 
        var span = document.getElementsByClassName("close")[0];
        var modal = document.getElementById('SendTestEmailModal');        

        // When the user clicks on the button, open the modal 
        $('#btnSendTestEmail').click(function () {
            //$("#SendTestEmailModal").show();
            modal.style.display = "block";
        });

        // When the user clicks on <span> (x), close the modal
        span.onclick = function () {
            modal.style.display = "none";
        }

        // When the user clicks anywhere outside of the modal, close it
        window.onclick = function (event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }                                      
    });
</script>

Here's the partial view (EmailTestForm.cshtml): 这是局部视图(EmailTestForm.cshtml):

@model EmailTemplateEditor.Models.TestEmail

<body>

    @using (Html.BeginForm("SendTestEmail", "EmailTemplate", FormMethod.Post, new { id = "SendTestEmailForm" }))
    { 
        <table class="module">                     
            <tr>
                <td>Subject:</td>
                <td> @Html.TextBoxFor(m => m.Subject, new { id = "txtSubject" })</td>
                <td>Body:</td>
                <td>@Html.TextBoxFor(m => m.Body, new { id = "txtBody" })</td>
            </tr>
        </table>
        <br /><br />
        <input type="submit" value="Send Test Email" class="btn btn-default" onclick="SubmitAction()"/>
    }

    <script>
        $(document).ready(function () {            
            function SubmitAction() {                
                var data = $("#SendTestEmailForm").serialize();

                $.ajax({
                    type: "Post",
                    url: "@Url.Action("SendTestEmail", "EmailTemplate")",
                    data: data,
                    success: function (partialResult) {
                        $("#modal-content").html(partialResult);
                    }
                })
            }            
        });
    </script>
</body>

Here's the relevant controller function (EmailTemplateController.cs): 这是相关的控制器功能(EmailTemplateController.cs):

[HttpPost]
public void SendTestEmail(TestEmail model)
{   
    if (ModelState.IsValid)
    {               
        Worker.SendMCTestEmail(model.SenderEmail, model.RecipientEmail, model.SenderName, model.RecipientName, model.Subject, model.Body, model.RecipientFirstName, model.RecipientLastName);       
    }   
}

Here's the css for the modal (in main.css): 这是模态的css(在main.css中):

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 100000000; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.close {
    color: #000;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM