我正在嘗試使用依賴注入Autofac
或Ninject
,但最終我的客戶端驗證失效。依賴注入正在阻止客戶端驗證MVC3
這裏是我的接口:
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"/>
但驗證不工作。
爲什麼說這是防止客戶端驗證的IOC容器? – jgauffin 2012-01-02 07:48:16
@jgauffin:這似乎是一個猜測,很可能是一個不正確的。 – 2012-01-02 07:52:14
然後刪除猜測,你可能會得到更好的幫助。還要添加任何javascript錯誤以及生成的視圖的外觀。刪除依賴注入容器驗證的功能後, – jgauffin 2012-01-02 07:55:58