处理数据之前的表单重定向
Form Redirects before data is processed
我正在使用 EntityFramework(代码优先)模式制作 ASP.Net 核心 MVC。我有一个剃须刀页面,它呈现了所有表单输入的部分内容(删除了大部分易于阅读的 div)。这是 myPartial,在我的控制器
中提交调用 AddClub 方法
@using (Html.BeginForm("AddClub", "Club", FormMethod.Post, new { @class = "form-horizontal" }))
{
<div class="form-group">
<label class="control-label col-sm-3">Club Sponser:</label>
<div class="col-sm-4">
@Html.TextBox("ClubSponser", null, new { @class = "form-control", id = "ClubSponser", placeholder = "Enter club Sponser" })
</div>
</div>
<div class="btn-toolbar col-md-offset-7" role="group">
<button type="submit" onsubmit="AddClub("ClubName","ClubOwner","ClubCoach","ClubSponser")" class="btn btn-primary">Add Club</button>
<a href="@Url.Action("Index", "Home")" class="btn btn-danger">Cancel</a>
</div>
}
这是我的控制器AddClub()
[HttpPost]
public ActionResult AddClub(string ClubName,string ClubOwner,string ClubCoach,string ClubSponser)
{
Club club = new Club()
{
Name = ClubName,
Owner = ClubOwner,
Coach=ClubCoach,
Sponser=ClubSponser
};
clubRepo.AddClub(club);
return RedirectToAction("Index","Club");
}
这是我的服务Class实现接口
public async Task AddClub(Club club)
{
_context.Clubs.Add(club);
await _context.SaveChangesAsync();
}
在 Startup 服务中作为单例注入
services.AddSingleton<IClubRepo, ClubService>();
1) 我相信它正在发生,因为在我的服务中 Class 方法是 运行 异步的,这可能是原因(不确定)。我有这种预感,因为如果我不重定向它会完美地更新数据库
2) 我不想提出另一个问题,但我只想知道这是在 ASP.Net core/MVC
中提交表单的正确方法吗
您需要等待操作
[HttpPost]
public async Task<IActionResult> AddClub(string ClubName,string ClubOwner,string ClubCoach,string ClubSponser) {
Club club = new Club() {
Name = ClubName,
Owner = ClubOwner,
Coach=ClubCoach,
Sponser=ClubSponser
};
await clubRepo.AddClub(club);
return RedirectToAction("Index","Club");
}
为了在重定向前完成保存。
我正在使用 EntityFramework(代码优先)模式制作 ASP.Net 核心 MVC。我有一个剃须刀页面,它呈现了所有表单输入的部分内容(删除了大部分易于阅读的 div)。这是 myPartial,在我的控制器
中提交调用 AddClub 方法 @using (Html.BeginForm("AddClub", "Club", FormMethod.Post, new { @class = "form-horizontal" }))
{
<div class="form-group">
<label class="control-label col-sm-3">Club Sponser:</label>
<div class="col-sm-4">
@Html.TextBox("ClubSponser", null, new { @class = "form-control", id = "ClubSponser", placeholder = "Enter club Sponser" })
</div>
</div>
<div class="btn-toolbar col-md-offset-7" role="group">
<button type="submit" onsubmit="AddClub("ClubName","ClubOwner","ClubCoach","ClubSponser")" class="btn btn-primary">Add Club</button>
<a href="@Url.Action("Index", "Home")" class="btn btn-danger">Cancel</a>
</div>
}
这是我的控制器AddClub()
[HttpPost]
public ActionResult AddClub(string ClubName,string ClubOwner,string ClubCoach,string ClubSponser)
{
Club club = new Club()
{
Name = ClubName,
Owner = ClubOwner,
Coach=ClubCoach,
Sponser=ClubSponser
};
clubRepo.AddClub(club);
return RedirectToAction("Index","Club");
}
这是我的服务Class实现接口
public async Task AddClub(Club club)
{
_context.Clubs.Add(club);
await _context.SaveChangesAsync();
}
在 Startup 服务中作为单例注入
services.AddSingleton<IClubRepo, ClubService>();
1) 我相信它正在发生,因为在我的服务中 Class 方法是 运行 异步的,这可能是原因(不确定)。我有这种预感,因为如果我不重定向它会完美地更新数据库
2) 我不想提出另一个问题,但我只想知道这是在 ASP.Net core/MVC
中提交表单的正确方法吗您需要等待操作
[HttpPost]
public async Task<IActionResult> AddClub(string ClubName,string ClubOwner,string ClubCoach,string ClubSponser) {
Club club = new Club() {
Name = ClubName,
Owner = ClubOwner,
Coach=ClubCoach,
Sponser=ClubSponser
};
await clubRepo.AddClub(club);
return RedirectToAction("Index","Club");
}
为了在重定向前完成保存。