Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在DataContext与视图之间切换_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 在DataContext与视图之间切换

C# 在DataContext与视图之间切换,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在开发一个ASP.NET MVC应用程序,它使用两种不同的数据存储。我为每个连接创建了2个不同的上下文类别。我已经单独发了短信,两人都工作得很好 背景1: public class DbContextA : DbContext { public DbContextA () : base("name=DbContextA ") { } public virtual DbSet<Producto> Productos { get; s

我正在开发一个ASP.NET MVC应用程序,它使用两种不同的数据存储。我为每个连接创建了2个不同的上下文类别。我已经单独发了短信,两人都工作得很好

背景1:

public class DbContextA : DbContext
{
    public DbContextA ()
        : base("name=DbContextA ")
    {
    }

    public virtual DbSet<Producto> Productos { get; set; }
}

但我不知道怎么做。我将感激任何帮助

我不知道你为什么这样做,或者你想解决的业务问题是什么,我发现了解一些背景知识有助于我们提供更好的帮助

但就你的问题而言,我建议你尝试将问题分解成小块。调试
Index()
操作时,在
ViewData[“Context”]
中是否得到任何信息?如果没有,那么这就是您的第一个小问题。先把它修好


一旦你克服了这个问题,就创造下一个小问题。

我不确定你为什么要这样做,或者你试图解决的业务问题是什么,我发现了解一些背景知识有助于我们提供更好的帮助

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            private DbContextA db1 = new DbContextA();
            private DbContextB db2 = new DbContextB();
            string selStorageValue = form["StorageTypes"];
        // Assuming selStorageValue has some value.
            ViewBag.Productos = null;
            if (selStorageValue != null)
            {
                if(selStorageValue.ToString()=="1"){
                    ViewBag.Productos=db1.Productos.ToList();

                }
                else if(selStorageValue.ToString()=="2"){
                    ViewBag.Productos=db2.Productos.ToList();

                }
            }
            else{
                 ViewBag.Productos=db1.Productos.ToList();

            }
            return View(); //View must be binded

       }
但就你的问题而言,我建议你尝试将问题分解成小块。调试
Index()
操作时,在
ViewData[“Context”]
中是否得到任何信息?如果没有,那么这就是您的第一个小问题。先把它修好

一旦你克服了这个问题,创造下一个小问题

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            private DbContextA db1 = new DbContextA();
            private DbContextB db2 = new DbContextB();
            string selStorageValue = form["StorageTypes"];
        // Assuming selStorageValue has some value.
            ViewBag.Productos = null;
            if (selStorageValue != null)
            {
                if(selStorageValue.ToString()=="1"){
                    ViewBag.Productos=db1.Productos.ToList();

                }
                else if(selStorageValue.ToString()=="2"){
                    ViewBag.Productos=db2.Productos.ToList();

                }
            }
            else{
                 ViewBag.Productos=db1.Productos.ToList();

            }
            return View(); //View must be binded

       }
查看

    @{
       ViewBag.Title = "Title";
       Layout = " layout File address";
       List<Producto> ProductoList = (List<Producto>)ViewBag.Productos;
     }
@{
ViewBag.Title=“Title”;
Layout=“布局文件地址”;
List ProductoList=(List)ViewBag.Productos;
}
现在您已经看到Productos列表,现在使用您希望在视图中实现它的方式。 例如,创建一个选择框

<div id="someId">
  @Html.DropDownList("Productos", 
                     new SelectList
                     (
                       ProductoList, "ProductoId", "ProductoName"
                     ), 
                     new { @class = "form-control" })

</div>

@Html.DropDownList(“Productos”,
新选择列表
(
ProductoList,“ProductoId”,“ProductoName”
), 
新的{@class=“表单控制”})
查看

    @{
       ViewBag.Title = "Title";
       Layout = " layout File address";
       List<Producto> ProductoList = (List<Producto>)ViewBag.Productos;
     }
@{
ViewBag.Title=“Title”;
Layout=“布局文件地址”;
List ProductoList=(List)ViewBag.Productos;
}
现在您已经看到Productos列表,现在使用您希望在视图中实现它的方式。 例如,创建一个选择框

<div id="someId">
  @Html.DropDownList("Productos", 
                     new SelectList
                     (
                       ProductoList, "ProductoId", "ProductoName"
                     ), 
                     new { @class = "form-control" })

</div>

@Html.DropDownList(“Productos”,
新选择列表
(
ProductoList,“ProductoId”,“ProductoName”
), 
新的{@class=“表单控制”})

Muchisimas gracias,era justo lo que necesitaba!我不知道你说了什么,但还是要谢谢你,很高兴能帮上忙:-)Muchisimas gracias,era justo lo que necesitaba!我不知道你说了什么,不过还是要谢谢你,很乐意帮忙:-)