0
我正在使用简单的会员资格进行帐户注册,并且在用户注册时我需要存储额外的数据。我正在使用 Ninject 进行依赖注入,并且我已经有一个存储库,它获取状态列表以填充下拉列表。我可以将它作为参数传递给我的其他控制器中的构造函数,它工作正常。但是,当我尝试将它传递给我的帐户控制器时,它返回 null。显然,这个控制器和我的其他控制器之间存在一些差异,但我无法弄清楚它可能是什么。这就是我将存储库参数传递给构造函数的方式。
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>();

有没有其他人有这个问题?编辑添加 - 也非常感谢没有遇到此问题但有建议的人提供帮助。:-)

4

0 回答 0