在.NET';上的单个视图上加载多个模型的最简单方法;s MVC5

在.NET';上的单个视图上加载多个模型的最简单方法;s MVC5,.net,asp.net-mvc-5,.net,Asp.net Mvc 5,我完全是.NET和MVC5的初学者。我已经使用脚手架为我的web应用程序创建了所有视图/控制器/模型的目录集。它们工作得很好,每个都在相应的菜单上 但我想做的是使用Twitter引导的选项卡面板创建一个视图,其中一个选项卡将代表我的应用程序的目录 我一直在阅读,就我对这件事的有限知识而言,这本书展示了6种方法来做我想做的事情 这将是我的主控制器,所有其他控制器都依赖于它: using System; using System.Collections.Generic; using System.D

我完全是.NET和MVC5的初学者。我已经使用脚手架为我的web应用程序创建了所有视图/控制器/模型的目录集。它们工作得很好,每个都在相应的菜单上

但我想做的是使用Twitter引导的选项卡面板创建一个视图,其中一个选项卡将代表我的应用程序的目录

我一直在阅读,就我对这件事的有限知识而言,这本书展示了6种方法来做我想做的事情

这将是我的主控制器,所有其他控制器都依赖于它:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TestSite.Models;

namespace TestSite.Controllers
{
    public class ClientsController : Controller
    {
        private TestSiteDBContext db = new TestSiteDBContext();

        // GET: Clients
        public ActionResult Index()
        {
            return View(db.Clients.ToList());
        }

        // GET: Clients/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Client client = db.Clients.Find(id);
            if (client == null)
            {
                return HttpNotFound();
            }
            return View(client);
        }

        // GET: Clients/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Clients/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,name,logoName,status,creationDate")] Client client)
        {
            if (ModelState.IsValid)
            {
                db.Clients.Add(client);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(client);
        }

        // GET: Clients/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Client client = db.Clients.Find(id);
            if (client == null)
            {
                return HttpNotFound();
            }
            return View(client);
        }

        // POST: Clients/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,name,logoName,status,creationDate")] Client client)
        {
            if (ModelState.IsValid)
            {
                db.Entry(client).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(client);
        }

        // GET: Clients/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Client client = db.Clients.Find(id);
            if (client == null)
            {
                return HttpNotFound();
            }
            return View(client);
        }

        // POST: Clients/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Client client = db.Clients.Find(id);
            db.Clients.Remove(client);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
和其他控制器(如部门)应在客户的显示视图中显示其内容:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TestSite.Models;

namespace TestSite.Controllers
{
    public class DepartmentsController : Controller
    {
        private TestSiteDBContext db = new TestSiteDBContext();

        // GET: Departments
        public ActionResult Index()
        {
            return View(db.Departments.ToList());
        }

        // GET: Departments/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Department department = db.Departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        // GET: Departments/Create
        public ActionResult Create()
        {
            ViewBag.BranchList = new SelectList(db.Branches, "ID", "name");
            return View();
        }

        // POST: Departments/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "id,name,branchId,creationDate")] Department department)
        {
            if (ModelState.IsValid)
            {
                db.Departments.Add(department);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(department);
        }

        // GET: Departments/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Department department = db.Departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        // POST: Departments/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "id,name,branchId,creationDate")] Department department)
        {
            if (ModelState.IsValid)
            {
                db.Entry(department).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(department);
        }

        // GET: Departments/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Department department = db.Departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        // POST: Departments/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Department department = db.Departments.Find(id);
            db.Departments.Remove(department);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
我想问你关于这6种方法中我应该首先关注哪一种的建议。谢谢

您的最佳选择:

  • 视袋

    ViewBag还用于将数据从控制器传递到视图。它是ControllerBase类中的一个动态属性,这就是它不需要对数据类型进行类型转换的原因

控制器 看法
@ViewBag.MyProperty

部分视图
用于需要在多个视图中共享相同代码(Razor和HTML代码)的情况。有关PartialView的更多详细信息,请访问此处。我认为这里的情况并非如此。

这篇文章讨论了一个类似的问题 堆栈溢出被证明是有用的。 我采用的方法是使用两个不同的模型创建两个局部视图,并将其中一个渲染为另一个。 邮报上说要用两种方法中的任何一种

@{Html.RenderPartial(“PartialView”,ViewBag.Controller);}

或者
@{Html.RenderPartial(“PartialView”,Model.Model)}
但对我有效的是使用

@{Html.RenderAction(“PartialView”、“controller”);}

这是该帖子的链接,希望我能有所帮助-

public class MyController : Controller
{
    public ActionResult Index()
    {
        ViewBag.MyProperty = 5;

        return View();
    }
}
<h1>@ViewBag.MyProperty</h1>