如何使用一个控制器使用多个模型 [英] How to work with multiple model with one controller

查看:80
本文介绍了如何使用一个控制器使用多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC的新手,我正在MVC创建我的第一个现场项目,我想知道如何使用多个模型和一个控制器。



我有三个或更多的模型类然后我将创建另一个包装类,其中包含多个模型类的对象,之后我将创建一个控制器,我想要所有这些操作,如添加,编辑,详细信息,删除所有一个控制器中的那些类。那么,谁能告诉我这个怎么用呢。





PLZ HELP

Hi, I am new in MVC, and i am creating a my first live project in MVC, i wanted to know how to work with multiple model and one controller.

I have three or more model classes and then i will create another wrapper classes that contains the objet of multiple model class after that i will create a controller, and i want all those actions like add, edit, details,delete for all those classes in one controller. so, can anyone tell me about this how to work with this.


PLZ HELP

推荐答案

1.定义一个将收集多个模型对象列表的类

2.在控制器中访问该对象类列表并发送给您的视图对象此模型列表of class

Ex。

First Model Class

1.Define a class which will collect more than one models list of object
2.In a controller access that list of object class and send to your view object this model list of class
Ex.
First Model Class
namespace MvcApplication1.Models
{
    public class Class1
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}





第二模型类



Second Model Class

namespace MvcApplication1.Models
{
    public class Class2
    {
        public int Id { get; set; }
        public string Category { get; set; }
    }
}



集合类和DbContext


Collection class and DbContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class ClassCollection
    {
        public int Id { get; set; }
        public List<class1> FirstClassList { get; set; }
        public List<class2> SecondClassList { get; set; }
    }
    public class DbClass : DbContext
    {
        public DbSet<classcollection> ClassCollections { get; set; }
    }
}
</classcollection></class2></class1>





在控制器中定义



Define in Controller

using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class myController : Controller
    {

        DbClass db = new DbClass();
        public ActionResult Index()
        {
            var list = db.ClassCollections.ToList();
            return View();
        }

    }
}







强类型视图

模型类:ClassCollection(MvcApplication1.Models)

和你的脚手架模板:把它们中的任何一个像细节一样

查看页面




strongly-typed view
Model class: ClassCollection (MvcApplication1.Models)
And Your Scaffold template : take any of them like "Details"
View page

@model MvcApplication1.Models.ClassCollection
@using MvcApplication1.Models 
@{
    ViewBag.Title = "Index";
}

Index
 ClassCollection 
<div>
    First model class value
   @foreach (Class1 item in @Model.FirstClassList)
   {
       <span>  Id:@item.Id</span>
       <span>Name:@item.Name</span>
   }   
</div>
<div>
     Second model class value
   @foreach (Class2 item in @Model.SecondClassList)
   {
       <span>  Id:@item.Id</span>
       <span>Name:@item.Category</span>
   }   
</div>


非常简短:

为您定义一个界面通常适合的控制器。然后为不同的需求实现这个接口并支持工厂实例化。



我或多或少确定SA会做出更好的解决方案。



问候
In very very short:
Define an interface for your controller which fits generally. Afterwards implement this interface for the different requirements and support a factory to instantiate.

I’m more or less sure SA will respond much better solution.

Regards


这篇关于如何使用一个控制器使用多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆