來自WebForms讓我的頭部繞過一些MVC的東西不太直觀,我正在努力尋找一種從控制器彈出的方式(取決於回髮結果)。 。MVC 5 - 從控制器中提升Bootstrap Modal
模態
<div id="PopupModal" class="modal fade in out">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" aria-hidden="true" type="button" data-dismiss="modal">×</button>
<h4 class="modal-title" id="ModalTitle"></h4>
</div>
<div class="modal-body" id="ModalBody">
<h5 id="ModalBodyText"></h5>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
</div>
</div>
</div>
這是我怎麼會募集的WebForms
Private Sub ModalValidation(Message As String)
Try
Dim SB As New StringBuilder
SB.Append("$(document).ready(function(){")
SB.Append("$('#PopupModal').modal();")
SB.Append("var vBody = document.getElementById('ModalBodyText');")
SB.Append("vBody.innerHTML = '" & Message & "';")
SB.Append("var vTitle = document.getElementById('ModalTitle');")
SB.Append("vTitle.innerHTML = 'Validation';")
SB.Append("vTitle.style.color = 'orange';")
SB.Append("});")
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ValidationShowModal", SB.ToString, True)
Catch ex As Exception
Dim vError As New SendError
vError.MailError(60, PageName, ex)
End Try
End Sub
我怎樣才能提高從控制器相同事件的事件?
感謝
編輯=非常感謝維托爾·薩爾加多指着我在正確的方向
將此添加到控制器 - 指數
Function Index() As ActionResult
Dim vPopup As New UploadFilesResult
If Not Session("PopupMessage") Is Nothing Then
vPopup.PopupMessage = Session("PopupMessage")
Else
vPopup.PopupMessage = "None"
End If
ViewData("UploadFilesResult") = vPopup
Return View("BlueImpMinView")
End Function
將此添加到控制器 - 的ActionResult(其中動作正常完成)
Session("PopupMessage") = "The file was successfully uploaded!"
Return Redirect("/blueimp/Index#Completed")
已添加隱藏字段到視圖
@code
Dim vPopup As MVPTest.UploadFilesResult = CType(ViewData("UploadFilesResult"), MVPTest.UploadFilesResult)
Dim vPopupMessage As String = vPopup.PopupMessage
@Html.TextBox("MessageTB", vPopupMessage, New With {.type = "hidden"})
End Code
和模態
<script>
window.onload = function() {
var vType = location.hash;
var vMessage = document.getElementById('MessageTB').value;
switch (vType) {
case '#Completed':
//run code for completed modal
$(document).ready(function() {
$('#PopupModal').modal();
var vBody = document.getElementById('ModalBodyText');
vBody.innerHTML = vMessage;
var vTitle = document.getElementById('ModalTitle');
vTitle.innerHTML = 'Success';
vTitle.style.color = 'green';
});
break;
case '#Error':
//run code for error modal
break;
case '#Validation':
//run code for validation modal
}
}
</script>
爲什麼從控制器中提出它?你爲什麼要追加腳本?控制器應該以這種方式影響視圖:它應該將任何必要的數據傳遞給視圖,然後視圖應該弄清楚如何顯示自身(可能涉及客戶端代碼)。確保正確理解MVC體系結構以及View和Controller的角色以及它們應該如何交互。 – mason
嗨梅森 - 控制器是進行所有數據處理的地方。我知道ScriptManager是WebForms的一部分,而不是MVC的一部分,我只是想在數據處理完成後(或者並非如此)找到一種提升Modal的方法。我不得不說我還沒有很好地理解這個架構,但是我前幾天才跳過 - 我學到的唯一方法就是跳進去嘗試一些東西...... – gchq
我會建議你在然後控制器將模型(包括驗證)傳遞給視圖並讓視圖解決問題。 –