English 中文(简体)
我如何把物体带入我的VIewModel的施工者? ASP.NET MVC 3, Ninject
原标题:How do I pass an object into the constructor of my VIewModel? ASP.NET MVC 3, Ninject

我先利用Bob Cravens博客中描述的存放模式来创建我的申请,但我只是一线新鲜事,现在仍在寻找我的路。 我想将我的数据服务对象引入我的观点Model的构造者,以便我能够制造一个选择性的分子,并在我看来制造一个 drop子。 然而,我似乎无法得到工作约束,每次我提出它期待/执行无足轻重的建筑者的看法! 我尝试了各种办法,在《SO》上用答案,但徒劳无益。 希望将受到高度赞赏。

观点:

public class ServerCreateViewModel
{
    public SelectList Companies { get; private set; }

    public ServerCreateViewModel()
    {

    }

    public ServerCreateViewModel(DataService _dataService)
    {

        Companies = new SelectList(_dataService.Companies.All(), "Id", "CompanyName");

    }

Ninject模块:

        Bind<DataService>().ToSelf()
            .InRequestScope();

        var _dataService = Kernel.Get<DataService>();

        Bind<ServerCreateViewModel>()
            .ToSelf()
            .WithConstructorArgument("_dataService", _dataService);

        //Bind<ServerCreateViewModel>()
        //    .ToSelf()
        //    .WithConstructorArgument("_dataService", ctx => ctx.Kernel.Get<DataService>());

主计长:

    public ActionResult Create(ServerCreateViewModel viewModel)
    {

        return View(viewModel);
    } 
最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 页: 1

观点模型(事实上所有模型)应当只是一些数据。 它们不应取决于任何商业逻辑、服务等。

控制权者有责任聚居模型,并将其传递意见。

public class ServerCreateViewModel
{
    public SelectList Companies { get; private set; }
}

public ActionResult Create()
{
    var viewModel = new ServerCreateViewModel
    {
        Companies = new SelectList(_dataService.Companies.All(), "Id", "CompanyName")
    };

    return View(viewModel);
} 

数据服务应当注入控制器,而不是视模型。

问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签