In meiner ASP.NET MVC-Anwendung können Benutzer Projekte veröffentlichen, und sie werden in der Datenbank gespeichert. Wie üblich gibt es eine Indexansicht für die Projekte, in der Benutzer die Liste aller verfügbaren Projekte zusammen mit einigen Aktionslinks neben ihnen sehen können (Details/Übernehmen).
Wenn ein Benutzer auf den Aktionslink "Übernehmen" klickt, wird er gebeten, ein paar Worte darüber zu sagen, warum er teilnehmen möchte (Anschreiben). Nachdem er sich angemeldet hat, wird eine Anwendung in der Datenbank gespeichert, die einen eindeutigen ApplicationId
, den UserId
des Benutzers und den ProjectId
enthält, an dem er sich beworben hat, damit ich den Überblick über alle Anwendungen behalten kann.ASP.NET Identity die E-Mail des Benutzers von UserId abrufen
public class Application
{
public int ApplicationId { get; set; }
public int ProjectId { get; set; }
public string UserId { get; set; }
public string CoverLetter { get; set; }
}
In der Ansicht „Details“, um alle Daten des Projekts wird angezeigt und verwaltet, es zu bekommen alle Anschreiben anzuzeigen und die Benutzer-ID des für dieses Projekt angewendet.
Cover letters:
@foreach (var item in Model.Applications)
{
<tr>
<td>
<br/>
@Html.DisplayFor(modelItem => item.CoverLetter) |
@Html.DisplayFor(modelItem => item.UserId) |
</td>
</tr>
}
Die UserId
und CoverLetter
aus der Anwendung ist jedoch erfolgreich abgerufen, was ich brauche auf den UserId
basierte den Namen des Benutzers und E-Mail aus der Datenbank abgerufen werden.
Edit: hier ist die gesamte ProjectsController:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using System.Web.Security;
using Leepio.Models;
using Microsoft.ApplicationInsights.Web;
using Microsoft.AspNet.Identity;
namespace Leepio.Controllers
{
public class ProjectsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Projects
[Authorize(Roles = "companyRole, studentRole")]
public ActionResult Index(string SortOrder, string SortBy, string Page)
{
ViewBag.SortOrder = SortOrder;
ViewBag.SortBy = SortBy;
var projects = db.Projects.ToList();
var model = new ApplicationTwoViewModel
{
Model1 = new List<Project>(projects),
Model2 = new Application
{
UserId = User.Identity.GetUserId(),
ProjectId = 11,
CoverLetter = "asdf",
ApplicationId = 23,
}
};
switch (SortBy)
{
case "Title":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.Title).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.Title).ToList();
break;
default:
break;
}
break;
case "ApplicationDeadline":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.ApplicationDeadline).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.ApplicationDeadline).ToList();
break;
default:
break;
}
break;
case "Duration":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.Duration).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.Duration).ToList();
break;
default:
break;
}
break;
case "HourlyRate":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.HourlyRate).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.HourlyRate).ToList();
break;
default:
break;
}
break;
case "TotalProjectCost":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.TotalProjectCost).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.TotalProjectCost).ToList();
break;
default:
break;
}
break;
case "City":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.City).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.City).ToList();
break;
default:
break;
}
break;
case "RequiredPresencePercent":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.RequiredPresencePercent).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.RequiredPresencePercent).ToList();
break;
default:
break;
}
break;
case "Language":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.Language).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.Language).ToList();
break;
default:
break;
}
break;
case "RequiredSkills":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.RequiredSkills).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.RequiredSkills).ToList();
break;
default:
break;
}
break;
default:
projects = projects.OrderBy(x => x.Title).ToList();
break;
}
ViewBag.TotalPages = Math.Ceiling(db.Projects.ToList().Count()/10.0);
int page = int.Parse(Page == null ? "1" : Page);
ViewBag.Page = page;
model.Model1 = projects;
model.Model1 = model.Model1.Skip((page - 1) * 10).Take(10).ToList();
return View(model);
}
private UserManager<IUser<> userManager;
// GET: Projects/Details/5
[Authorize(Roles = "companyRole, studentRole")]
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
// GET: Projects/Create
[Authorize(Roles= "companyRole")]
public ActionResult Create()
{
return View();
}
// POST: Projects/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "companyRole")]
public ActionResult Create([Bind(Include = "UserId,ProjectId,Title,ApplicationDeadline,Duration,HourlyRate,TotalProjectCost,City,RequiredPresencePercent,Language,RequiredSkills,Description")] Project project)
{
if (ModelState.IsValid)
{
project.UserId = User.Identity.GetUserId();
db.Projects.Add(project);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
// GET: Projectts/Edit/5
[Authorize(Roles = "companyRole")]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
// POST: Projects/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "companyRole")]
public ActionResult Edit([Bind(Include = "ProjectId,UserId,Title,ApplicationDeadline,Duration,HourlyRate,TotalProjectCost,City,RequiredPresencePercent,Language,RequiredSkills,Description")] Project project)
{
if (ModelState.IsValid)
{
if (project.UserId == User.Identity.GetUserId())
{
db.Entry(project).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return HttpNotFound("Project is not yours!");
}
return View(project);
}
// GET: Projects/Delete/5
[Authorize(Roles = "companyRole")]
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
// POST: Projects/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Authorize(Roles = "companyRole")]
public ActionResult DeleteConfirmed(int id)
{
Project project = db.Projects.Find(id);
if (project.UserId == User.Identity.GetUserId())
{
db.Projects.Remove(project);
db.SaveChanges();
}
else
{
return HttpNotFound("Project is not yours!");
}
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Muss ich tatsächlich etwas in der ApplicationController
ändern? Ich bin mir nicht sicher, wie ich das machen soll.
Nach dem Projekt zu finden. Verwenden Sie die 'UserId' des Projekts, um das Benutzerobjekt aus dem' UserManager.FindByIdAsync' zu holen. Sie müssen Ihr Rückgabemodell ändern, um diese Informationen an Ihre Ansicht zurückzugeben. – Nkosi