【问题标题】:Refresh PartialView in MVC Controller在 MVC 控制器中刷新局部视图
【发布时间】:2020-01-06 16:39:41
【问题描述】:

我在提交将在我的控制器中处理的表单后尝试刷新我的部分视图。问题是,每当我尝试从我的控制器刷新它时,我都会被重定向到包含部分视图内容的空白页面。

局部视图

@model Smarty.Viewmodels.BugViewModel

<div class="modal fade" id="bugModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Bug Reporting Tool</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span>&times;</span>
                </button>
            </div>
            <form asp-controller="Smarty" asp-action="SendBugReport" enctype="multipart/form-data">

                @if (!string.IsNullOrWhiteSpace(ViewBag.message))
                {
                    if (!ViewBag.IsError)
                    {
                        <span class="border border-success text-success">@ViewBag.message</span>
                    }
                    else
                    {
                        <span class="border border-danger text-danger">@ViewBag.message</span>
                    }
                }
                <div class="modal-body">
                    <label asp-for="Description" class="control-label"></label>
                    <textarea asp-for="Description" class="form-control"></textarea>
                    <span asp-validation-for="Description" class="text-danger"></span>

                    <label asp-for="FormFile" class="control-label"></label><br />
                    <input asp-for="FormFile" type="file" />
                    <span asp-validation-for="FormFile" class="text-danger"></span>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Schliessen</button>
                    <button type="submit" id="BugReportBtn" class="btn btn-success">Bug Report senden</button>
                </div>
            </form>
        </div>
    </div>
</div>

控制器

public async Task<IActionResult> SendBugReport(BugViewModel viewModel)
   {
    //Process Form

    return PartialView("BugModal", viewModel);
   }

提前致谢!

【问题讨论】:

  • 是否需要局部视图出现在页面刷新上?如果不是,您可以只返回您想要导航到的View (View("SomeView", viewModel);)
  • @Dortimer 不,我想用错误消息、成功消息等内容刷新部分视图。
  • 这是一个完整的 POST。您需要使用 AJAX,如下所示:stackoverflow.com/questions/35202804/…
  • 如果您只想更新部分视图,您应该考虑为它创建一个完整的页面/控制器并将其放置在 iframe 中。 (或通过javascript处理表单...)否则只需返回...部分将与页面一起刷新。
  • 我删除了我提交的答案。 @SteveGreene 是正确的。我不相信有其他选择。

标签: c# asp.net-mvc asp.net-core


【解决方案1】:

我被重定向到包含部分视图内容的空白页面。

这是意料之中的,因为您使用 return PartialView() 将返回简单的部分 html 以将其呈现到视图中。

我想用错误消息、成功消息等内容刷新部分视图

您无法从SendBugReport 操作中获取@ViewBag.message,它是从主页的操作中传递的。

正如评论所说,首先,您可以使用ajax将表单提交给SendBugReport action。然后该action将messageisError json数据返回给ajax成功函数。最后,根据isError的值在视图上渲染message

1.部分视图(Views/Shared/BugModal.cshtml)

@model BugViewModel

<div class="modal fade" id="bugModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Bug Reporting Tool</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span>&times;</span>
                </button>
            </div>
            <form id="myForm" asp-controller="Smarty" asp-action="SendBugReport" enctype="multipart/form-data">
                <div id="result"></div>
                <div class="modal-body">
                    <label asp-for="Description" class="control-label"></label>
                    <textarea asp-for="Description" class="form-control"></textarea>
                    <span asp-validation-for="Description" class="text-danger"></span>

                    <label asp-for="FormFile" class="control-label"></label><br />
                    <input asp-for="FormFile" id="FormFile" name="FormFile" type="file" />
                    <span asp-validation-for="FormFile" class="text-danger"></span>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Schliessen</button>
                    <button type="button" id="BugReportBtn" class="btn btn-success">Bug Report senden</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
<script>
    $('#BugReportBtn').on('click', function (event) {

        var url = "/Smarty/SendBugReport";

        var description = document.getElementById("Description").value;
        var fileInput = $('#FormFile')[0];
        var formFile = fileInput.files[0];

        var formData = new FormData();
        formData.append("Description", description);
        formData.append("FormFile", formFile);

        $.ajax({
            type: "POST",
            url: url,
            data: formData,
            dataType: "json",
            processData:false,
            contentType: false,
            success: function (data) {
                if (!data.isError) {
                    $("#result").html("<span class='border border-success text-success'>" + data.message + "</span>");
                } else {
                    $("#result").html("<span class='border border-danger text-danger'>" + data.message + "</span>");
                }              
                $('#bugModal').modal('show');

            }

        });


    });
</script>

2.动作

[HttpPost]
    public async Task<JsonResult> SendBugReport(BugViewModel viewModel)
    {
        //Process Form

        string message;
        bool isError;

        //set data to message and isError
        return Json(new { message, isError });

    }

【讨论】:

    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    相关资源
    最近更新 更多