Neo*_*Neo 2 .net c# asp.net asp.net-mvc
我有一个Index如下所示的页面,单击按钮后我有一些代码将数据保存到数据库中,然后它会显示在详细信息页面上。
但我的代码应该在完成数据库保存操作后才转到详细信息页面;在那之前我想显示加载程序图像;我怎样才能做到这一点?
目前我正在使用 begin post to post 方法并绑定所有模型,不使用任何 ajax 调用。如何在流程完成之前显示加载程序图像并渲染到详细信息页面?
索引.cshtml
@model Dev.Models.DeviceReg
@using (Html.BeginForm("AddAsync", "Home", FormMethod.Post))
{
<div class="panel panel-primary">
<div class="panel-body">
<div class="row">
<div class="col-md-6">
<h4 id="aa">Name</h4>
<label>Select</label>
<table>
<tr>
<td>
@Html.DropDownListFor(m => m.Name, (SelectList)ViewBag.Name, "---Select Name---")
</td>
</tr>
</table>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h4 id="aa">Model</h4>
<label>Select</label>
<table>
<tr>
<td>
@Html.DropDownListFor(m => m.Type, (SelectList)ViewBag.TypeName, "---Select Type---")
</td>
</tr>
</table>
</div>
</div>
<div class="panel-footer" align="left">
<button type="submit" id="save" class="btn btn-success">
<span class="glyphicon glyphicon-arrow-right"></span> save
</button>
</div>
</div>
}
Run Code Online (Sandbox Code Playgroud)
HomeController.cs
public async Task<ActionResult> AddAsync(DeviceReg deviceRegistration)
{
foreach (var deviceId in collection)
{
// save device information into database
Models.Device newDevice = new Models.Device()
{
Id = Guid.NewGuid(),
DeviceTypeId = deviceRegistration.DeviceType,
PlatformId = deviceRegistration.PlatformType,
DeviceId = deviceId,
};
_repository.InsertDevice(newDevice);
_repository.Save();
}
return View("Details", deviceRegistration);
}
Run Code Online (Sandbox Code Playgroud)
详细信息.cshml
@model Dev.Models.DeviceReg
<body style="background-color:black">
<div class="panel panel-primary">
<div class="panel-heading" align="center">
<h2 class="panel-title">Details</h2>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-6">
<h3><label>Current Data</label></h3>
<br />
</div>
</div>
<div class="row">
<div class="col-md-6">
<h4 id="aa">Name</h4>
<label>@Model.Name</label>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h4 id="aa">Type</h4>
<label>@Model.TypeName</label>
</div>
</div>
<hr />
<br />
<label>Your process is running.</label>
<br />
<div class="row">
<div class="col-md-6">
<h3><label>Status</label></h3>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div style="clear: both">
<h2 style="float: left">10</h2>
<h6 style="float: right">Active Number</h6>
</div>
</div>
</div>
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
好吧,如果您需要在提交表单时显示加载程序,您可以使用 javascript 函数来显示它,例如
@using (Html.BeginForm("AddAsync", "Home", FormMethod.Post, new { onsubmit = "showLoader(this);" }))
{
...
}
Run Code Online (Sandbox Code Playgroud)
加JS
<script>
var showLoader = function(form){
$("<div />").css({
'position' : 'fixed',
'left' : 0,
'right' : 0,
'bottom' : 0,
'top' : 0,
'background' : '#0020ff36',
'z-index' : '99',
'text-align' : 'center'
}).appendTo($("body"))
.append(
$("<img />").attr("src", "https://mir-s3-cdn-cf.behance.net/project_modules/disp/35771931234507.564a1d2403b3a.gif")
);
}
</script>
Run Code Online (Sandbox Code Playgroud)
或者通过jquery事件,比如
<script>
$("form").submit(function(){
//show loader
});
</script>
Run Code Online (Sandbox Code Playgroud)
此代码示例https://dotnetfiddle.net/gfEVSE
但是,关于您在评论中对问题的澄清,如果没有 Javascript 或其他附加请求,就不可能显示带有保存进度的详细信息页面。
没有 ajax 但每 N 秒有一次额外请求的示例
[HttpPost]
public ActionResult AddAsync(SampleViewModel deviceRegistration)
{
Task.Run(()=>
{
//Saving to DB
});
return RedirectToAction("Details", id = deviceRegistration.id);
}
public ActionResult Details(int id)
{
var isObjectExistInDb = checkIfExistInDb(id);
if (!isObjectExistInDb){
return View("ShowLoader", id);
}
return View(object);
}
Run Code Online (Sandbox Code Playgroud)
在 ShowLoader.cshtml 中,您需要每 N 秒重新加载一次页面。
使用ajax,代码会更加清晰、漂亮。如果您需要 ajax 示例,请告诉我:)
| 归档时间: |
|
| 查看次数: |
14766 次 |
| 最近记录: |