我正在尝试将依赖注入应用于Autofac
或Ninject
,但最终我的客户端验证无效。
这是我的界面:
public interface IPersonRepository
{
IQueryable<Person> Persons { get; }
}
以下是模型:
class EFPersonRepository : IPersonRepository
{
private readonly Context _context;
public EFPersonRepository(Context context)
{
_context = context;
}
public IQueryable<Person> Persons
{
get { return _context.Persons; }
}
}
这是控制器:
public class PersonController : Controller
{
private readonly IPersonRepository _personRepository;
public PersonController(IPersonRepository personRepository)
{
_personRepository = personRepository;
}
public ActionResult Create()
{
var person=new Person();
return View(person);
}
}
申请开始:
protected void Application_Start()
{
// ApplyDependeny();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
private void ApplyDependeny()
{
var context = new Context();
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterModule(new DependencyInjector(context));
DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
}
客户端参考:
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
的web.config:
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
但验证无效。
答案 0 :(得分:0)
添加此客户端引用
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
答案 1 :(得分:0)
您正在注册您的上下文和所有控制器,但您忘记注册您的存储库。
builder.RegisterType<EFPersonRepository>().As<IPersonRepository>();