我对 MVC3 相当陌生,我正在尝试创建一个订单屏幕,用户可以在其中登录并查看他们最近的订单。这证明了一个问题,因为我试图从数据库中提取用户名值并将它们与当前登录的用户进行比较,以便每个用户只能查看自己的订单而不能查看其他人。但是,我正在努力将数据库中的值转换为字符串,以便将它们与登录的用户名进行比较。
这是我的模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Assignment2.Models
{
public partial class Order
{
[ScaffoldColumn(false)]
public int OrderId { get; set; }
[ScaffoldColumn(false)]
public System.DateTime OrderDate { get; set; }
[ScaffoldColumn(false)]
public string Username { get; set; }
[Required(ErrorMessage = "First Name is required")]
[DisplayName("First Name")]
[StringLength(160)]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
[DisplayName("Last Name")]
[StringLength(160)]
public string LastName { get; set; }
[Required(ErrorMessage = "House Number/Name")]
[StringLength(70)]
public string House { get; set; }
[Required(ErrorMessage = "Street Name is required")]
[StringLength(70)]
public string Address1 { get; set; }
public string Address2 { get; set; }
[Required(ErrorMessage = "City is required")]
[StringLength(40)]
public string City { get; set; }
[Required(ErrorMessage = "County is required")]
[StringLength(40)]
public string County { get; set; }
[Required(ErrorMessage = "Post Code is required")]
[DisplayName("PostCode")]
[StringLength(10)]
public string Postcode { get; set; }
[Required(ErrorMessage = "Country is required")]
[StringLength(40)]
public string Country { get; set; }
[Required(ErrorMessage = "Phone number is required")]
[StringLength(24)]
public string Phone { get; set; }
[Required(ErrorMessage = "Email Address is required")]
[DisplayName("Email Address")]
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
ErrorMessage = "Email is is not valid.")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[ScaffoldColumn(false)]
public decimal Total { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
}
}
这就是我试图用我的控制器做的事情
using System.Web.Security;
using System.Web.Mvc;
using Assignment2.Models;
namespace Assignment2.Controllers
{
public class OrderController : Controller
{
private RevolutionEntities db = new RevolutionEntities();
public ViewResult Index()
{
string loggedinuser = User.Identity.Name;
string username =
from a in db.Orders
where a.Username == loggedinuser
select a;
return View();
}
}
谢谢杰米