部分视图内部分视图未发布

时间:2011-11-22 14:15:17

标签: jquery asp.net-mvc-3 razor

对于整个MVC的做事方式我还是比较陌生的,所以请耐心等待。

我目前正在开发一个现有的MVC网站,根据需要添加管理功能。有一个主要的管理视图。所有其他CRUD操作都由加载到单个管理视图中的部分视图处理。到目前为止,我没有添加所有这些部分视图的问题。现在,有两个表:Email_Queue和Smtp_Server。业务规则声明您必须具有Smtp_Server记录才能创建Email_Queue记录。

因此,为了使用户更好,我可以选择在Email_Queue的Create partial视图中创建Smtp_Server。我可以在模态弹出窗口中打开Smtp_Server的创建局部视图...我可以获得所有的验证(在模型中设置)来触发......我可以获取取消按钮来关闭模态弹出窗口。但是,我似乎无法将Smtp_Server局部视图上的提交按钮发布。

所有这一切的最终目标最终都是发布,然后在我刚刚添加的新Smtp_server的Email_Queue局部视图更新中有一个下拉列表。现在,我很满意Smtp_Server只是保存我输入的内容。

所以,这里有一些代码。首先,Email_Queue局部视图(我删除了额外的字段以使其更短):

@model Models.Email_QueueModel

<div class="main_column_leftfull">
<!--=========Graph Box=========-->
<div class="box expose">
    <!-- A box with class of expose will call expose plugin automatically -->
    <div class="header">
        Create E-Mail Queue
    </div>
    <div class="body">
        <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
        <script type="text/javascript">

            function CancelAddSmtpServer() {
                var windowElement = $('#QueueCreate');
                windowElement.hide();
            }

            $(document).ready(function() {

            var windowElement = $('#QueueCreate');
            var undoButton = $('#opener-popup');
            undoButton
                .bind('click', function(e) {
                    windowElement.data('tWindow').open();
                    undoButton.hide();})
                .toggle(!windowElement.is(':visible'));
                    windowElement.bind('close', function() {
                    undoButton.show();

                });
            });

            function CloseWindow() {
                var windowElement = $('#QueueCreate').data('tWindow');
                windowElement.close();
            }

        </script>
        @using (Ajax.BeginForm("_Create", new AjaxOptions() { UpdateTargetId = "subForm", HttpMethod = "Post" }))
        {
            <fieldset>
                @Form.HiddenID("DepartmentId")
                <div class="editor-label">
                    @Html.LabelFor(model => model.QueueName)
                    @Html.EditorFor(model => model.QueueName, new { @class = "textfield" })
                    @Html.ValidationMessageFor(model => model.QueueName)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.EmailServerConfig)
                    @Html.DropDownList("EmailServerConfig", "Select E-Mail Server Configuration...")
                    @Html.ValidationMessageFor(model => model.EmailServerConfig)
                </div>

                <div id="opener-popup" class="editor-label" style="cursor:pointer;">
                    Add SMTP Server
                </div>

                <div id="popup_content" title="Popup Title" style="display:none;">
                    @Html.Partial("../Email_SmtpServer/_QueueCreate")
                </div>

                @(Html.Telerik().Window()
                    .Name("QueueCreate")
                    .Modal(true)
                    .Title("Add SMTP Server")
                    .Scrollable(false)
                    .Draggable(true)
                    .Resizable()
                    .Visible(false)
                    .Content
                    (
                        @<text>
                            @Html.Partial("../Email_SmtpServer/_QueueCreate", new DATEL.MM.Models.Email_SmtpServerModel())
                        </text>
                    )
                )

                <br />
                <p>
                    @Form.Submit(value: "Create E-Mail Queue")
                   <input id="btnCancelEmail_QueueCreate" type="button" value="Cancel" class="button" />
                </p>
            </fieldset>
        }
        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>
    </div>
</div>

接下来,我为模态弹出窗口创建的局部视图:

@model Models.Email_SmtpServerModel

<div class="main_column_leftfull">
<!--=========Graph Box=========-->
<div class="box expose">
    <!-- A box with class of expose will call expose plugin automatically -->
    <div class="header">
        Create SMTP Server
    </div>
    <div class="body">
        <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>


        @using (Ajax.BeginForm("_QueueCreate", new AjaxOptions() { UpdateTargetId = "subForm", HttpMethod = "Post" }))
        {
            <fieldset>
                @Form.HiddenID("SMTPServerId")
                <div class="editor-label">
                    @Html.LabelFor(model => model.ServerName)
                    @Html.EditorFor(model => model.ServerName, new { @class = "textfield" })
                    @Html.ValidationMessageFor(model => model.ServerName)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.ServerDesc)
                    @Html.EditorFor(model => model.ServerDesc, new { @class = "textfield" })
                    @Html.ValidationMessageFor(model => model.ServerDesc)
                </div>

                <br />
                <p>
                    @Form.Submit(value: "Create SMTP Server")

                    <input id="btnCancelEmail_SmtpServerQueueCreate" type="button" value="Cancel" class="button" onclick="CloseWindow();" />
                </p>
            </fieldset>
        }
    </div>
</div>

最后,这是_QueueCreate的控制器代码:

[HttpPost]
    public virtual PartialViewResult _QueueCreate(Email_SmtpServerModel model, FormCollection fc)
    {
        ViewBag.HasError = "none";
        try
        {
            string errorMessage = "";
            BusinessLogic.Email_SmtpServer dbESS = new BusinessLogic.Email_SmtpServer(AppManager.GetUser(User.Identity.Name).ConnectionString);
            model.SMTPServerId = System.Guid.NewGuid();
            model.CreatedDateGMT = DateTime.Now;
            model.CreatedUserId = AppManager.GetUser(User.Identity.Name).UserId;

            if (dbESS.Insert(model, out errorMessage))
            {
                ModelState.AddModelError("", errorMessage);
            }
            else
            {
                ViewBag.HasError = "none";
                return PartialView("../Email_Queue/_Create");
            }
        }
        catch
        {
            ViewBag.HasError = "true";
            return PartialView("../Email_Queue/_Create");
        }
        return PartialView(model);
    }

如果我需要发布更多代码,请告知我们。

TL; DR版:好吧,看看标题。

1 个答案:

答案 0 :(得分:1)

你不能强制提交按钮点击提交

<input type="submit" name="btnSubmit" value="Submit Form" onclick="this.form.submit()" />

或者只是使用jQuery查找onclick然后使用ajax发布

$('input[name="btnSubmit"').click(function() {
//you will need to get form variables

$.ajax({
type:"POST",
data: { id = var1, parameter = var2 }
datatype: "json",
success: function (data){
//whatever needs done
},
error: function(){
alert("error");
}

});

MVC控制器

public jsonresult somename(string id, string parameter)
{ 
return Json("SUCCESS");
}

public jsonresult somename(FormCollection fc)
{
strind id = fc["id"];
string parameter = fc["parameter"];
return Json("SUCCESS");
}