我使用简单的会员资格进行帐户注册,我需要在用户注册时存储额外的数据。我正在使用Ninject进行依赖注入,我已经有了一个存储库,可以获取填充下拉列表的状态列表。我可以将此作为参数传递给我的其他控制器中的构造函数,它可以正常工作。但是,当我尝试将其传递给我的帐户控制器时,它返回null。显然,这个控制器和我的其他控制器之间存在一些差异,但我无法弄清楚它可能是什么。这就是我将repository参数传递给构造函数的方法。
public class AccountController : Controller
{
private readonly IStateRepository sRepository;
public AccountController(IStateRepository sRepo)
{
sRepository = sRepo;
}
public ActionResult Register(RegisterModel model)
{
var stateQuery = sRepository.States.Select(m => new SelectListItem
{
Value = SqlFunctions.StringConvert((double)m.StateID),
Text = m.State1,
Selected = m.StateID.Equals(0)
});
model.StateList = stateQuery.AsEnumerable();
//more code to submit the registration
return View(model);
}
}
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Address")]
public string Address1 { get; set; }
[Display(Name = "Apt #")]
public string Address2 { get; set; }
[Required]
[Display(Name = "City")]
public string City { get; set; }
[Required]
[Display(Name = "State")]
public string State { get; set; }
[Required]
[Display(Name = "Zip")]
public string Zip { get; set; }
public string StateId { get; set; }
public IEnumerable<SelectListItem> StateList { get; set; }
}
注册视图中的下拉列表
@Html.DropDownListFor(model => model.StateId, Model.StateList)
存储库界面
public interface IStateRepository
{
IQueryable<State> States { get; }
}
存储库
public class EFStateRepository : IStateRepository
{
//private EFDbContext context = new EFDbContext();
private Entities context = new Entities();
public IQueryable<State> States
{
get { return context.States; }
}
}
结合
ninjectKernel.Bind<IStateRepository>().To<EFStateRepository>();
还有其他人有这个问题吗?编辑添加 - 还感谢那些没有遇到此问题,但有建议的人。 : - )