我對整個MVC的處事方式還比較陌生,所以請耐心等待。在部分視圖中不能張貼的局部視圖
我目前正在研究現有的MVC網站,根據需要添加管理功能。有一個主要的管理視圖。所有其他CRUD操作都通過加載到單個管理視圖中的部分視圖進行處理。至此,我沒有添加所有這些部分視圖的問題。現在有兩個表:Email_Queue和Smtp_Server。業務規則規定,您必須先創建Smtp_Server記錄,然後才能創建Email_Queue記錄。
因此,爲了讓用戶更好,我可以選擇在創建Email_Queue的部分視圖中創建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版本:好吧,看看標題。
我其實最終使用jQuery來獲取部分視圖發佈。一切都節省了,現在我只需要關閉模式彈出窗口並更新成功下拉菜單。 – user1059903