C# ASP.NET MVC多个模型一组创建、查看、编辑和删除页面
对于有多个模型要在一组视图中显示的情况,我在计算如何实现创建、编辑和删除页面时遇到了一个问题。 我正在使用Visual Studio 2013和MVC 5。我还使用了最新的实体框架6.1.1 到目前为止,我已经得到了以下模型:C# ASP.NET MVC多个模型一组创建、查看、编辑和删除页面,c#,asp.net-mvc,asp.net-mvc-5,entity-framework-6.1,C#,Asp.net Mvc,Asp.net Mvc 5,Entity Framework 6.1,对于有多个模型要在一组视图中显示的情况,我在计算如何实现创建、编辑和删除页面时遇到了一个问题。 我正在使用Visual Studio 2013和MVC 5。我还使用了最新的实体框架6.1.1 到目前为止,我已经得到了以下模型: public class CompositeModel { public int ID { get; set; } public MediaPlan MediaPlanModel { get; set; } pub
public class CompositeModel
{
public int ID { get; set; }
public MediaPlan MediaPlanModel { get; set; }
public ContactInfo ContactInfoModel { get; set; }
public MediaPlanDate MediaPlanDateModel { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
}
public class MediaPlan
{
public int ID { get; set; }
public string Client { get; set; }
public string Product { get; set; }
public string AE { get; set; }
public string Supervisor { get; set; }
public string Traffic { get; set; }
}
public class ContactInfo
{
public int ID { get; set; }
public string CompanyName { get; set; }
public string CompanyContact { get; set; }
public string CompanyAddress { get; set; }
public string CompanyPhoneNumber { get; set; }
public string CompanyEmail { get; set; }
}
public class MediaPlanDate
{
public int ID { get; set; }
public string Client { get; set; }
public string Product { get; set; }
public string AE { get; set; }
public string Supervisor { get; set; }
public string Traffic { get; set; }
}
使用代码优先的方法正确创建了数据库
我确实自动生成了CompositeModelsController.cs:
public class CompositeModelsController : Controller
{
private MprContext db = new MprContext();
// GET: CompositeModels
public ActionResult Index()
{
//var compositeModel = new CompositeModel();
//compositeModel.MediaPlanModel = new MediaPlan();
//compositeModel.ContactInfoModel = new ContactInfo();
//compositeModel.MediaPlanDateModel = new MediaPlanDate();
//return View(compositeModel.ToList());
return View(db.Composites.ToList());
}
// GET: CompositeModels/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CompositeModel compositeModel = db.Composites.Find(id);
if (compositeModel == null)
{
return HttpNotFound();
}
return View(compositeModel);
}
// GET: CompositeModels/Create
public ActionResult Create()
{
return View();
}
// POST: CompositeModels/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,RowVersion")] CompositeModel compositeModel)
{
if (ModelState.IsValid)
{
db.Composites.Add(compositeModel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(compositeModel);
}
// GET: CompositeModels/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CompositeModel compositeModel = db.Composites.Find(id);
if (compositeModel == null)
{
return HttpNotFound();
}
return View(compositeModel);
}
// POST: CompositeModels/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,RowVersion")] CompositeModel compositeModel)
{
if (ModelState.IsValid)
{
db.Entry(compositeModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(compositeModel);
}
// GET: CompositeModels/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CompositeModel compositeModel = db.Composites.Find(id);
if (compositeModel == null)
{
return HttpNotFound();
}
return View(compositeModel);
}
// POST: CompositeModels/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
CompositeModel compositeModel = db.Composites.Find(id);
db.Composites.Remove(compositeModel);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
我没想到它会起作用,但事实并非如此
我能够使索引工作
我还创建了编辑器模板:
@model Online_Mpr.Models.MediaPlan
<h2>MediaPlan</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>MediaPlan</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.Client, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Client, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Client, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AE, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AE, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AE, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Supervisor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Supervisor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Supervisor, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Traffic, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Traffic, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Traffic, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model Online\u Mpr.Models.MediaPlan
MediaPlan
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
MediaPlan
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.ID)
@LabelFor(model=>model.Client,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Client,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Client,“,new{@class=“text danger”})
@LabelFor(model=>model.Product,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Product,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Product,“,new{@class=“text danger”})
@LabelFor(model=>model.AE,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.AE,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.AE,“,new{@class=“text danger”})
@LabelFor(model=>model.Supervisor,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Supervisor,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Supervisor,“,new{@class=“text danger”})
@LabelFor(model=>model.Traffic,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Traffic,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Traffic,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
其他模型也是如此
然后我有了创建视图:
@model Online_Mpr.Models.CompositeModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CompositeModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.MediaPlanModel)
@Html.EditorFor(model => model.MediaPlanDateModel)
@Html.EditorFor(model => model.ContactInfoModel)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model Online\u Mpr.Models.CompositeModel
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
复合模型
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@EditorFor(model=>model.MediaPlanModel)
@EditorFor(model=>model.MediaPlanDateModel)
@EditorFor(model=>model.ContactInfoModel)
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
我正计划对“编辑”视图执行类似的操作
当我转到“创建”视图时,它会显示所有元素的编辑框,但如何对将在数据库中创建记录的控制器进行编码。
我希望在Create视图中只有一个“Create”按钮可以创建所有四条记录
任何帮助都是非常感谢的,因为我在互联网上搜索类似案例的信息,但没有找到任何
另外,如果您可以提供代码示例。它们不需要详细说明,但核心思想会引导我
谢谢,
-Arek您的
复合模型本质上是一个视图模型。此类不应具有Id
属性,也不应以任何方式实际绑定到实体框架(不要为其添加DbSet
)。它所需要做的就是包含要编辑的其他模型实例的属性。您还应该向此类添加一个构造函数,该构造函数可以更新类中的所有其他模型:
public class CompositeModel
{
public CompositeModel()
{
MediaPlanModel = new MediaPlan();
ContactInfoModel = new ContactInfo();
MediaPlanDateModel = new MediaPlanDate();
}
...
}
然后在您的操作的后期版本中创建操作。您只需分别保存每个子模型:
[HttpPost]
public ActionResult Create(CompositeModel model)
{
if (ModelState.IsValid)
{
db.MediaPlans.Add(model.MediaPlanModel);
db.ContactInfos.Add(model.ContactInfoModel);
db.MediaPlanDates.Add(model.MediaPlanDateModel)
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
对于您的编辑版本:
[HttpPost]
public ActionResult Edit(CompositeModel model)
{
if (ModelState.IsValid)
{
db.Entry(model.MediaPlanModel).State = EntityState.Modified;
db.Entry(model.ContactInfoModel).State = EntityState.Modified;
db.Entry(model.MediaPlanDateModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
你想做什么?您只需要将记录列表一次性发布到控制器?如果是,您可以查看是否要一次编辑所有模型?或者一次只编辑一个模型?我想一次编辑所有模型。但后来我意识到,分散使用多个模型可能毫无意义,我可能应该创建一个大模型。它将完成同样的事情。因为我第一次这样做,我知道它会起作用的。你怎么认为?谢谢你的回复,谢谢你的回复。但目前我正在考虑放弃多模型方法,我将只做一个大模型,基本上完成相同的事情;只有在对应用程序有意义时才这样做MediaPlan
和MediaPlanDate
在我看来已经像是应该组合在一起的东西,但是ContactInfo
似乎像是应该单独使用的东西,因为它可以在其他场景和与不同实体的其他关系中重用。