如何在局部视图MVC中自动重新加载视图

时间:2017-12-01 09:08:03

标签: jquery ajax asp.net-mvc-5

我希望在部分视图mvc中操作后自动重新加载视图。当我在局部视图中保存位置时,我希望主视图中的值自动重新加载。你是如何在视图之间进行的?视图在局部视图中,保存后,主视图应刷新

public PartialViewResult _AddPost(int idOrder, AddPositionViewModel viewModel)
        {
            var findOrder = db.Order.Find(idOrder);

            if (ModelState.IsValid)
            {

                OrderPosition position = new OrderPosition { Description = viewModel.Description };
                db.OrderPosition.Add(position);
                //db.Entry(position).State = System.Data.Entity.EntityState.Unchanged;
                findOrder.OrderPositionList.Add(position);
                db.SaveChanges();
                ViewBag.Information = position;                  

                if (ViewBag.Information != null)
                {
                    TempData["Add-Post"] = string.Format("Odpowiedz użytkownika {0} została dodana!", User.Identity.Name);
                    //HttpContext.Items["Info"] = string.Format("Odpowiedz użytkownika {0} została dodana!", User.Identity.Name);
                }

            }
            Thread.Sleep(2000);
            return PartialView();

        }
@model HelpDesk.ViewModels.AddPositonViewModel

@{
    ViewBag.Title = "Add Post";
    Layout = null;
}

<link href="~/Content/font-awesome.min.css" rel="stylesheet" />
<script src="~/Scripts/tinymce/tinymce.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/jquery-2.2.3.min.js"></script>




<div class="container-fluid">
    @using (Ajax.BeginForm(new AjaxOptions()
    {
        UpdateTargetId = "divformResult",
        HttpMethod = "Post"

    }))


    {
        @Html.AntiForgeryToken()
        @Html.Hidden("IdOrder")
        <div class="form-horizontal">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-1" })
                <div class="col-md-10">
                    @Html.TextAreaFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                </div>
<pre><div class="col-md-10">
                    @Html.TextAreaFor(model => model.DataPlaningFinish, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.DataPlaningFinish, "", new { @class = "text-danger" })
                </div>
  <div class="form-group  has-success">
                @Html.LabelFor(model => model.IdStatusOrder, "Status Order", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(x => x.IdStatusOrder, Model.StatusOrderSelectListItem, "Status", new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.IdStatusOrder, "", new { @class = "text-danger" })
                </div>
            </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10 ">
                    <input type="submit" value="Save" class="btn btn-danger" />
                </div>
            </div>
        </div>
    }

</div>

我想在动作PartialView后自动更新此值(DataPlaningFinish,IdOrder)

@model AplikacjaHelpDesk.ViewModels.ListOrdersUserViewModel
@using AplikacjaHelpDesk.Infrastructure
@{
    ViewBag.Title = "List Orders Users";
    Layout = "~/Views/Shared/_LayoutAdministracja.cshtml";
}

<div class="container-fluid">
    <img src="~/Content/Images/Layout/Home.png" />
    <a href="link">
        @Html.MvcSiteMap().SiteMapPath()
    </a>
    <h2><span class="glyphicon glyphicon-user"></span> List Orders </h2>
    <br /><br />
    <div id="divLoading" class="panel panel-primary text-center text-primary" style="display:none;">
        <h3>Please wait for post!</h3>
    </div>
    <div id="divLoadingForm" class="panel panel-primary text-center text-primary" style="display:none;">
        <h3>Please wait for form</h3>
    </div>
    @if (ViewBag.Information != null)
            {
        <div class="alert alert-warning"><h4>@TempData["Add-Post"]</h4></div>
    }*@

    <table class="table table-responsive table-striped" style="text-combine-upright:all;">
        <tr style="text-transform: uppercase; text-combine-upright:all;">
            <th>
                <label>Numer Order</label>
            </th>
            <th>
                <label>Acceptance Date</label>
            </th>
            <th>
                <label>Date of planned completion of the order</label>
            </th>
            <th>
                <label>Data finish</label>
            </th>
            <th style="width: 160px;"></th>
            <th style="width: 160px;"></th>
        </tr>

        @foreach (var item in Model.ListOrdersUser)
        {

            <tr class="panel panel-primary">
                <td>
                    <h5>Nuber Orders: @Html.DisplayFor(modeItem => item.IdOrder)</h5>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DateAccept )
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DataPlaningFinish)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DataFinish)
                </td>
                <td>
                    @Ajax.ActionLink("Show Post Order", "_ListPost", new { idOrder = @item.IdOrder }, new AjaxOptions()
               {
                   HttpMethod = "GET",
                   LoadingElementId = "divLoading",
                   UpdateTargetId = "divPosition",
                   InsertionMode = InsertionMode.Replace

               }, new { @class = "btn btn-primary" })
                </td>
                <td>


                    @Ajax.ActionLink("Add Answer", "_AddPost", new { idZlecenia = @item.IdZlecenia }, new AjaxOptions()
               {
                   HttpMethod = "GET",
                   LoadingElementId = "divLoadingForm",
                   UpdateTargetId = "divDodajPozycje",
                   InsertionMode = InsertionMode.Replace

               }, new { @class = "btn btn-primary" })
                </td>

            </tr>
            ...
            <tr id="divDodajPozycje"></tr>
        }
    </table>

</div>

1 个答案:

答案 0 :(得分:1)

在AjaxOptions中使用OnSuccess回调

调用一个函数来刷新你的内容

例如

@Ajax.ActionLink(
    "", 
    "", 
    null,
    new AjaxOptions { 
        HttpMethod = "POST",
        OnSuccess = "updateView()" 
    }
)

回调函数

function updateView()
{
// here write your own code to reload
}

或者您也可以使用RedirectToAction

完成你所需要的一种粗俗方法是:

函数中,将RedirectToAction替换为:

var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Home");
return Json(new { Url = redirectUrl });


OnComplete = "location.reload()",   <-- this additional comma should be removed

OnSuccess= "OnAjaxSuccess"

然后在Javascript中添加一个函数

function OnAjaxSuccess(data) {
    window.location.href = data.Url;
}