使用MVC中的viewmodel在单个视图中动态添加多个模型 [英] Dynamically add multiple models in a single view using viewmodel in MVC

查看:113
本文介绍了使用MVC中的viewmodel在单个视图中动态添加多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我在MVC(Razor)中使用EF 4.x的要求创建了一个视图模型,如下所示: -



  public   class  ClassSectionSubjectViewModel {
public ClassTable ClassTable { get ; set ; }
public 列表< Section>部分{获取; set ; }
public 列表< Subject>主题{获取; set ;}
public ClassSectionSubjectViewModel()
{
/// 在特定视图中动态创建这些模型的代码,每个
/// 上面的类有其ID和不同的属性
}
}





此外,在一个视图中,我使用以下代码行进行调试,

但我不知道是否动态模型生成会很好用

(请在此处纠正我)



  public  ActionResult ClassSectionSubjectCreationVM()
{
ClassTable classTable = new ClassTable();
列表< ClassTable> classTableList = db.ClassTables.ToList();
var classTables = classTableList;
var sectionList = db.Sections.ToList();
var subjectList = db.Subjects.ToList();

var viewModel = new ClassSectionSubjectViewModel();

return 查看(viewModel);
}





最后,我想要做的是在从第一个(默认)模型中选择值后动态添加其他模型即如果我的模型(默认)在viewmodel中是ClassTable(如上面的VM类所示)那么它应该在下拉列表中绑定来自DB的值(使用EF),即


4.x)



2.从上面的下拉列表中选择值后,它必须显示相应的模型,即其值与上面步骤相同的部分并添加它到其他数据库或在sql服务器的VIEW中(如果有任何其他建议然后请帮助)



3.以相同的方式将主题类添加到主题类中并将其添加到其他表的DB或在Sql Server的VIEW中(如果有任何其他意见那么请帮助)



完成所有这些步骤后,用户可以进行CRUD操作。



请帮助我,因为我是MVC的首发。



先谢谢!!

解决方案

你不能在一个视图中实现多个模型,无论它是否是动态的。您可以将不同模型的多个局部视图实现为动态或无动态。


下面是下拉更改时调用操作的代码

< script type =   text / javascript> 

document )。ready( function (){


I''ve created a view model as per my requirement in MVC(Razor) using EF 4.x, as shown below :-

public class ClassSectionSubjectViewModel {
      public ClassTable ClassTable { get; set; }
      public List<Section> Sections { get; set; }
      public List<Subject> Subjects { get; set;}
      public ClassSectionSubjectViewModel()
      {
       ///what to code for dynamic creation of these models in a particular view each of
       ///the above class has its ID and different properties
      }
}



Also, in a view I was debugging with following lines of codes,
but I have no idea whether it would work fine with dynamic model generation
(please rectify me in this also)

public ActionResult ClassSectionSubjectCreationVM()
    {
        ClassTable classTable = new ClassTable();
        List<ClassTable> classTableList = db.ClassTables.ToList();
        var classTables = classTableList;
        var sectionList = db.Sections.ToList();
        var subjectList = db.Subjects.ToList();

        var viewModel = new ClassSectionSubjectViewModel();

        return View(viewModel);
    }



Finally, what I want to do is to add other model dynamically after selecting value from first(default) model i.e

1.if my model (default) in viewmodel is ClassTable (as shown in above VM class) then it should bind the value from DB in dropdownlist (using EF 4.x)

2.after selecting value from the above dropdownlist it must show the respective model i.e Section with its values in the same way as in above step and add it to other DB or in a VIEW of sql server(if any other suggestion then plz help)

3.lastly the subject class in the same way and add it to DB of other table or in a VIEW of Sql Server (if any other opinion then plz help)

After completing all these step user can make CRUD operation.

Please, help me in this as I''m a starter in MVC.

Thanks In advance !!

解决方案

You can''t implement multiple models in one view whether it is dynamic or not. You can implement multiple partial views with different model as dynamic or without dynamic.


Below are the code for calling an action when Dropdown Change

<script type="text/javascript">


(document).ready(function () {


这篇关于使用MVC中的viewmodel在单个视图中动态添加多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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