[英]Modal window create from Partial View (ASP.NET MVC)
我在查看電子郵件列表的位置查看
我需要使用模態窗口和部分視圖在表中創建新記錄。
這是部分視圖的代碼
@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>
我在Partial View中進行Partial View和AJAX調用
這是發布方法的代碼
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>
我怎么能意識到這一點?
您可以使用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模式對話框
有關boostrap模態的參考,請參見此處: Bootstrap modal
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.