如何将临时数据从部分视图发送到其父视图?我需要有关成功保存到数据库的信息。请使用TempData或HttpContext.Items示例。问题是如何在差异视图(从部分视图到父视图)
之间发送有关成功的信息这是Controller
中的方法 [HttpPost]
[ValidateAntiForgeryToken]
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();
}
//Method Parent View
public ActionResult ListOrder(int? IdStatusOrder)
{
var ListAllOrder = db.Order.ToList();
var userId = User.Identity.GetUserId();
var viewodel = new ListOrdersUserViewModel()
{
ListOrdersUser = ListOrders
};
};
return View(viewodel);
}
在MVC中查看
@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> <strong>List Orders </strong></h2>
<br /><br />
<div id="divLoading" class="panel panel-primary text-center text-primary" style="display:none;">
<h3><strong>Please wait for post!</strong></h3>
</div>
<div id="divLoadingForm" class="panel panel-primary text-center text-primary" style="display:none;">
<h3><strong>Please wait for form</strong></h3>
</div>
@if (ViewBag.Information != null)
{
<div class="alert alert-warning"><h4><strong>@TempData["Add-Post"]</strong></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><strong>Nuber Orders: @Html.DisplayFor(modeItem => item.IdOrder)</strong></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 = "divAddPozycje",
InsertionMode = InsertionMode.Replace
}, new { @class = "btn btn-primary" })
</td>
</tr>
...
<tr id="divAddPozycje"></tr>
}
</table>
</div>
和PartialView - 表单
@model AplikacjaHelpDesk.ViewModels.AddPositionViewModel
@{
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>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 ">
<input type="submit" value="Send" class="btn btn-danger" id="formularz"/>
</div>
</div>
</div>
}
</div>
@*@if (TempData["Add-Post"] != null)
{
<div class="alert alert-warning">
<h4><strong>@TempData["Add-Post"];</strong></h4>
</div>
}*@
<script type="text/javascript">
tinymce.init({
selector: "textarea",
language: "pl",
theme: "modern",
fontsize_formats: "8pt 10pt 12pt 14pt 18pt 24pt 36pt 48pt 72pt",
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste",
"textcolor",
"colorpicker",
],
theme_advanced_fonts: "Arial=arial,helvetica,sans-serif;Courier New=courier new,courier,monospace;AkrutiKndPadmini=Akpdmi-n",
extended_valid_elements: 'i[class]',
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | forecolor backcolor | fontsizeselect"
});
</script>
答案 0 :(得分:0)
实施GET
版本_AddPost
,返回TempData
值:
[HttpGet]
[ValidateAntiForgeryToken]
public JsonResult _AddPost()
{
return Json(TempData["Add-Post"]);
}
修改原始ActionLink
并添加OnComplete
功能:
@Ajax.ActionLink("Add Answer", "_AddPost", new { idZlecenia = @item.IdZlecenia }, new AjaxOptions()
{
HttpMethod = "POST", // Not "GET"
LoadingElementId = "divLoadingForm",
UpdateTargetId = "divAddPozycje",
InsertionMode = InsertionMode.Replace,
OnComplete = "updateMyAlert();" // Add this
}, new { @class = "btn btn-primary" })
关注更新ActionLink
元素的新my-alert-text
:
@Ajax.ActionLink("", "_AddPost", new {}, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "my-alert-text",
InsertionMode = InsertionMode.Replace,
OnSuccess = "showMyAlertIfNeeded();"
}, new { id = "update-my-alert-action-link" })
使用相关的HTML和JavaScript补充上述内容:
<div class="alert alert-warning" id="my-alert" style="display: none;">
<h4><strong id="my-alert-text"></strong></h4>
</div>
<script>
function updateMyAlert() {
document.getElementById("update-my-alert-action-link").click();
}
function showMyAlertIfNeeded() {
var display = document.getElementById("my-alert-text").text ? "" : "none";
document.getElementById("my-alert").style.display = display;
}
</script>