首先 - 我對 c# 和 MVC .net 核心等非常陌生,所以我很難理解基本的東西是如何作業的。我有一個 Html.BeginForm 視圖,它在 Click 上填充模型的屬性,并從控制器呼叫一個函式,該函式驗證在 BeginForm 中設定的屬性。我希望控制器函式回傳一個區域視圖,該視圖將顯示為呼叫控制器的 ParentView 上的彈出視窗(根據驗證,它將是不同的彈出視窗)。有沒有辦法做到這一點?到目前為止,我有這樣的事情:
控制器:
PartialView CalledFromParentView(MyModel model)
{
//validate model properites
//..
if (model.isValid)
{
return PartialView("PartialViewA, model);
}
else
{
return PartialView("PartialViewB, model);
}
}
ActionResult CancelFromPartialViewA(MyModel model)
{
return View("ParentView", model);
}
ActionResult SubmitFromPartialViewA(MyModel model)
{
model.UpdateDB();
return View("SomeHomePage");
}
ActionResult OKFromPartialViewB(MyModel model)
{
return View("ParentView", model);
}
父視圖:
@model myModel
<html>
<body>
@using (Html.BeginForm("CalledFromParentView", "ControllerName", FormMethod.Post))
{
<h1> first model propery</h1>
@Html.TextBoxFor(model => model.FirstProperty)
<h1> second model proper</h1>
@Html.TextBoxFor(model => model.SecondProperty)
// etc.
<p>
<button type="submit"> submit properties</button>
</p>
}
</body>
</html>
部分視圖A:
@model myModel
<html>
<body>
<p> model properties are valid. Are you sure you want to submit the information?</p>
<input type="button"
value="submit"
onclick="location.href='<%: Url.Action("SubmitFromPartialViewA", "controllerName") %>'"/>
<input type="button"
value="cancel"
onclick="location.href='<%: Url.Action("CancelFromPartialViewA", "controllerName") %>'"/>
</body>
</html>
部分視圖B:
@model MyModel
<html>
<body>
<p>The model properties are invalid please fix the following information:</p>
// will list models invalid properties
<input type="button"
value="OK"
onclick="location.href='<%: Url.Action("OKFromPartialViewB", "ControllerName") %>'" />
</body>
</html>
正如我上面提到的,我希望部分視圖在父視圖上顯示為彈出視窗,而不是僅僅呈現部分視圖本身(這是我目前所擁有的)。蒂亞!!
uj5u.com熱心網友回復:
您可以使用 ajax 請求區域視圖,然后使用引導程式以模式呈現它。這是視圖。
@model MyModel
@using (Html.BeginForm("CalledFromParentView", "home", FormMethod.Post))
{
<h1> first model propery</h1>
@Html.TextBoxFor(model => model.FirstProperty)
<h1> second model proper</h1>
@Html.TextBoxFor(model => model.SecondProperty)
@*// etc.*@
<p>
<button type="submit" data-toggle="modal" data-target="#exampleModalLong"> submit properties</button>
</p>
}
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
</div>
</div>
</div>
@section Scripts{
<script>
$("form").submit(function (e) {
e.preventDefault()
console.log($(this).serialize())
$.ajax({
url: '@Url.Action("CalledFromParentView", "home")',
data: $(this).serialize(),
success: function (data) {
$('.modal-body').html(data)
$('#exampleModalLong').show();
},
error: function (err) {
}
})
});
</script>
}
下面是動作CalledFromParentView。
public ActionResult CalledFromParentView(MyModel model)
{
if (ModelState.IsValid)
{
return PartialView("PartialViewA", model);
}
else
{
return PartialView("PartialViewB", model);
}
}
它應該在 PartialViewA 中提供一個表單:
@model MyModel
<p> model properties are valid. Are you sure you want to submit the information?</p>
@using (Html.BeginForm("SubmitFromPartialViewA", "home", FormMethod.Post))
{
<h1> first model propery</h1>
@Html.TextBoxFor(model => model.FirstProperty)
<h1> second model proper</h1>
@Html.TextBoxFor(model => model.SecondProperty)
@*// etc.*@
<p>
<button type="submit" data-toggle="modal" data-target="#exampleModalLong"> submit</button>
<button type="button" data-dismiss="modal" aria-label="Close">
cancel
</button>
</p>
}
和結果。

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415875.html
標籤:
