我提出了一种方法,用于批准或拒绝用户注册表单中的注册请求。
当用户提交表单时,请求将变为待处理状态,然后管理员有权拒绝或批准该请求;但我的方法不能正常工作,因为没有任何事情发生。
当我点击拒绝或批准按钮时,应用程序只刷新页面。
有谁能告诉我我的代码有什么问题?代码片段如下。
这是审批控制器
public class ApprovedRegistrationController :BaseAdminController
{
// GET: Admin/ApproveRegistration
public ActionResult Index(string Status)
{ ViewBag.Status = (Status == null ? "P" : Status);
if (Status == null)
{ var register = objBs.registerBs.GetALL().Where(x => x.Approved == "P").ToList();
return View(register); }
else
{ var register = objBs.registerBs.GetALL().Where(x => x.Approved == Status).ToList();
return View(register); }}
public ActionResult Approve(int id)
{ try
{ var myRegistration = objBs.registerBs.GetByID(id);
myRegistration.Approved = "A";
objBs.registerBs.Update(myRegistration);
TempData["Msg"] = "Approved Successfully";
return RedirectToAction("Index"); }
catch (Exception e1)
{ TempData["Msg"] = "Approved Failed: " + e1.Message;
return RedirectToAction("Index");}}
public ActionResult Reject(int id)
{try
{ var myRegistration = objBs.registerBs.GetByID(id);
myRegistration.Approved = "R";
objBs.registerBs.Update(myRegistration);
TempData["Msg"] = "Rejected Successfully";
return RedirectToAction("Index");}
catch (Exception e1)
{ TempData["Msg"] = "Rejection Failed: " + e1.Message;
return RedirectToAction("Index");}}}
注册批准或拒绝视图
@model IEnumerable<XtechWebsite.Models.Registration>
@{
ViewBag.Title = "Index";}
<h2>Index</h2>
<script>
function ConfirmApprove() {
return confirm('Are u sure to accept it?');
}
function ConfirmReject() {
return confirm('Are u sure to reject it?');
}
</script>
<h3>Manage Registration<h3>
@if (TempData["Msg"] != null)
{<h3 @TempData["Msg"].ToString()</h3>
}
<h4>@if (ViewBag.Status == "P")
{<b>@Html.ActionLink("Pending Requests", "Index", new { Status = "P" })</b>
}
else
{
@Html.ActionLink("Pending Requests", "Index", new { Status = "P" })
}
@if (ViewBag.Status == "A")
{
<b>@Html.ActionLink("Approved Requests", "Index", new { Status = "A" })</b>
}
else
{
@Html.ActionLink("Approved Requests", "Index", new { Status = "A" })
}
@if (ViewBag.Status == "R")
{
<b>@Html.ActionLink("Rejected Requests", "Index", new { Status = "R" })</b>
}
else
{
@Html.ActionLink("Rejected Requests", "Index", new { Status = "R" })
}
</h4
<table class="table">
<tr>
<th>Approve/Reject</th>
<th>
@Html.DisplayNameFor(model => model.First_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Last_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Department_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Team_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Select_Members)
</th>
<th>
@Html.DisplayNameFor(model => model.Approved)
</th>
<th>
@Html.DisplayNameFor(model => model.Email_ID)
</th>
<th>
@Html.DisplayNameFor(model => model.Competition.CategoryName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Approve", "Approve", new { id = item.Registration_ID }, new { onclick = "return ConfirmApprove();" }) |
@Html.ActionLink("Reject", "Reject", new { id = item.Registration_ID }, new { onclick = "return ConfirmReject();" })
</td>
<td>
@Html.DisplayFor(modelItem => item.First_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Last_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Team_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Select_Members)
</td>
<td>
@Html.DisplayFor(modelItem => item.Approved)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Competition.CategoryName)
</td>
</tr> }
</table>
**Admin Base Contoller**
public class BaseAdminController : Controller
{
// GET: Admin/BaseAdmin
protected AdminBs objBs;
public BaseAdminController()
{
objBs = new AdminBs();
}
}
审批控制器中使用的registerbs类
public class RegisterBs
{
private RegisterDb objDb;
public RegisterBs()
{
objDb = new RegisterDb();
}
public IEnumerable<Registration> GetALL()
{
return objDb.GetALL();
}
public Registration GetByID(int Id)
{
return objDb.GetByID(Id);
}
public void Insert(Registration registration)
{
objDb.Insert(registration);
}
public void Delete(int Id)
{
objDb.Delete(Id);
}
public void Update(Registration registration)
{
objDb.Update(registration);
}
}
<b>Registration db class<b>
public class RegisterDb
{
private XtechContext db;
public RegisterDb()
{
db= new XtechContext();
}
public IEnumerable<Registration> GetALL()
{
return db.Registrations.ToList();
}
public Registration GetByID(int Id)
{
return db.Registrations.Find(Id);
}
public void Insert(Registration registration)
{
db.Registrations.Add(registration);
Save();
}
public void Delete(int Id)
{
Registration registration = db.Registrations.Find(Id);
db.Registrations.Remove(registration);
Save();
}
public void Update(Registration registration)
{
db.Entry(registration).State = EntityState.Modified;
}
public void Save()
{
db.SaveChanges();
}
}
答案 0 :(得分:2)
您不会在上下文中调用SaveChanges
,说明您的修改未持久存储到数据库的原因。
您可以在更新方法中添加对Save()
的通话。