我有一个控制器,我有两个与场景有关的标准ActionResults:索引和详细信息 在详细信息中,我会检查有关用户身份的各种条件,并检查其个人资料中可用的现有信用额度。如果满足所有条件,我将重定向到“详细信息”视图,并更新/添加一些记录。我想要的是在有效重定向(当然还有更新/添加过程)之前发出通知(例如:“嘿用户X,你有xx学分,完成你将消耗你的学分。你确定你想要的吗?继续y / n“)。如果没有停留在当前的索引视图上,如果是,请转到详细信息视图,并在详细信息ActionResult中执行所有更新/插入操作。 在Index视图中,我使用标准ActionLink导航到Detail View:
@Html.ActionLink(" View this Candidate details ...", "Details", "Candidates", new { id = item.IdCV }, new { @class = "btn btn-default fa fa-link" })
控制器中的操作详细信息为:
// GET: Candidates/Details/5
// Restrict by role:
//[Authorize(Roles = "Admin, Candidate, Employer")]
public ActionResult Details(int? id)
{
if (User.Identity.IsAuthenticated)
{
//obtaining the user profile info
var jobs = new Jobs();
var currentuser = User.Identity.GetUserId();//UserManager.FindById(User.Identity.GetUserId());
JobShopEntities db = new JobShopEntities();
var TheUser = db.AspNetUsers.Where(u => u.Id == currentuser)
.Select(u => new
{
ID = u.Id,
Email = u.Email,
Username = u.UserName,
Surrname = u.Surname,
Name = u.Name,
Role = u.Role,
CreditBalance = u.CreditBalance
}).Single();
var idul = TheUser.ID;
var username = TheUser.Username;
var name = TheUser.Name;
var surrname = TheUser.Surrname;
var credit = TheUser.CreditBalance;
var CreditUsed = 5;
var NewCredit = credit - CreditUsed; //This value I want to update in AspNetUsers.CreditBalance field
var role = TheUser.Role;
var operatia = "Display CV detail";
int? cvul = id;
int? jobul = null;
//testing the user
if (role == 3)
{
//if role is ok, testing the credit balance
if (credit > 10)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Candidate candidate = db.Candidate.Find(id);
if (candidate == null)
{
return HttpNotFound();
}
//update credits in AspNetUsers.CreditBalance
UpdateCredit(idul, username, NewCredit);
//save in audit journal CreditJournal.CreditsUsed
SaveAudit(operatia, idul, cvul, jobul, CreditUsed);
return View(candidate);
}
else
{
//redirect to something
return RedirectToAction("NoCredit", "Jobs");
}
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
return RedirectToAction("Login", "Account");
}
}
有关的更新和插入方法是:
UpdateCredit(idul, username, NewCredit);
和
SaveAudit(operatia, idul, cvul, jobul, CreditUsed);
在从主菜单输入创建视图之前,我也有类似的要求:
@Html.ActionLink("Add a new Job", "New", "Jobs")
这里我还检查了New ActionResult中的(几乎)相同的条件(好的,这里有两种ActionResult:New()和[HttpPost] New())
L.E。
最后,我找到了一种简单的方法,但有一些问题:
@ Html.ActionLink(“Detalii ...”,“详细信息”,“候选人”,新{id = item.IdCV},新{onclick =“返回确认('查看候选人详细信息,您将消耗5个学分从x可用!
你想继续吗?')“,@ class =”btn btn-default fa fa-link“})
@ Html.ActionLink(“新工作”,“新工作”,“工作”,新{onclick =“返回确认('对于添加新工作,您将从x中获得5个学分!
你是否想继续?')“})
对于这两种方法,我想要一种格式化消息的方法(例如我插入的方法,没有效果),并为信用卡注入消息文本值,因为它们在Controller中。
无论如何第一个例子有效,但第二个例子有问题:链接是用一个参数(不知道原因)修改的:
http://localhost:23442/Jobs/New
为: