首先--我對c#和MVC.net core等非常陌生,所以我很難理解基本的東西是如何作業的。 我有一個Html.BeginForm的視圖,它在點擊時填充模型的屬性,并從控制器中呼叫一個函式,驗證BeginForm中設定的屬性。我希望控制器函式能回傳一個部分視圖,該視圖將作為彈出視窗出現在呼叫控制器的ParentView上(它將根據驗證的結果出現不同的彈出視窗)。有什么方法可以做到這一點嗎? 到目前為止,我有這樣的方法:
控制器: 。PartialView CalledFromParentView(MyModel model)
{
//驗證模型屬性
//..
如果(model.isValid)
{
回傳PartialView("PartialViewA, model)。
}
否則
{
回傳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);
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
上級視圖:
@model myModel
<html>
<body>/span>
@using (Html.BeginForm("CalledFromParentView", "ControllerName", FormMethod.Post))
{
<h1>第一個模型屬性</h1>
@Html.TextBoxFor(model => model.FirstProperty)
<h1>第二個模型適當</h1>/span>
@Html.TextBoxFor(model => model.SecondProperty)
//等等。
<p>
<button type="submit"/span>> 提交屬性</button>/span>
</p>/span>
}
</body>
</html>/span>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
PartialViewA:
@model myModel
<html>
<body>/span>
<p>模型屬性有效。你確定要提交資訊嗎?</p>
<input type="button"
value="submit"
onclick="location.href='<%: Url.Action("SubmitFromPartialViewA", "controllerName") %>' "/>
<input type="button"。
value="cancel"/span>
onclick="location.href='<%: Url.Action("CancelFromPartialViewA", "controllerName") %>' "/>
</body>
</html>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
PartialViewB:
@model MyModel
<html>
<body>
<p>模型屬性無效請修復以下資訊:</p>/span>
//將列出模型無效的屬性
<input type="button"
value="OK"。
onclick="location.href='<%: Url.Action("OKFromPartialViewB", "ControllerName" ) %>'" />
</body>
</html>/span>
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
正如我上面提到的,我希望部分視圖以彈出的形式出現在父視圖上,而不是僅僅呈現部分視圖本身(這就是我目前的情況)。 TIA!!
uj5u.com熱心網友回復:
你可以使用ajax來請求部分視圖,然后用bootstrap在modal中渲染它。這是視圖。
。@model MyModel
@using (Html.BeginForm("CalledFromParentView", "home", FormMethod.Post))
{
<h1>第一個模型屬性</h1>
@Html.TextBoxFor(model => model.FirstProperty)
<h1>第二個模型適當</h1>/span>
@Html.TextBoxFor(model => model.SecondProperty)
@*//等.*@
<p>
< button type="submit" data- toggle="modal" data-target="#exampleModalLong"> 提交屬性</button>。
</p>/span>
}
<!-- 模態 --> }
< div class="modal fade"/span> id="exampleModalLong"/span> tabindex="- 1" role=" dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
< div class="modal-dialog" role="document">
<div class="modal-content"/span>>
<div class="modal-header"/span>>
< h5 class="modal-title" id="exampleModalLongTitle" > Modal標題</h5>
< button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true"/span>> ×</span>
</button>
</div>
<div class="modal-body">
</div>/span>
</div>/span>
</div>/span>
</div>/span>
@section Scripts{
<script>
$("form").submit(function (e) {
e.preventDefault()
console.log($(this).serialize()
$.ajax({
url: '@Url.Action("CalledFromParentView", "home")'/span>。
data: $(this).serialize()。
success。function (data) {
$('.modal-body').html(資料
$('#exampleModalLong').show()。
},
error。function (err) {
}
})
});
</script>/span>
}
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
這里是動作CalledFromParentView。
public ActionResult CalledFromParentView(MyModel model)
{
if (ModelState.IsValid)
{
return PartialView("PartialViewA"/span>, model)。
}
else[/span
{
return PartialView("PartialViewB", model);
}
它應該在PartialViewA中提供一個表單:
@model MyModel
<p> 模型屬性是有效的。你確定要提交這些資訊嗎?</p>。
@using (Html.BeginForm("SubmitFromPartialViewA", "home", FormMethod.Post)
{
<h1> 第一個模型屬性</h1>。
@Html.TextBoxFor(model => model.FirstProperty)
<h1> 第二個模型屬性</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">
取消
</button>
</p>
}
而結果是。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/320338.html
標籤:

