我是 C# 和 MVC 的新手,正在从 PHP 背景进入 Visual Studio 环境。我正在摸索(很多),但到了那里。我需要帮助来理解一些基本概念,毫无疑问,这是一项非常笨拙的工作,但帮助我解决这个问题将帮助我理解事情是如何运作的。
我有一个 SQL 数据库,通过添加 ADO.NET 实体类模型连接到 VS2012 web 项目 (C#)。可以说那一端没有问题,自动生成的默认 CRUD View 也没有问题。
用例:
我想要一个网页,向用户展示从数据库中提取的项目列表。这已经在默认的索引 View 中实现了,但我还希望该列表可以立即编辑,而不必为每个单独的项目输入编辑 View 。因此,我将列表 HTML 更改为表单,并为“更新”提交设置了一个 ActionName
。这也没有问题:
型号:
namespace Library3.DAL {
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class PrmTbl_Level {
public PrmTbl_Level() {
this.Tbl_Books = new HashSet<Tbl_Book>();
}
[Required]
[Display(Name = "ID")]
public int LevelID { get; set; }
[Required]
[Display(Name = "Description")]
public string LevelDesc { get; set; }
[Required]
[Display(Name = "Entered By")]
public string EnteredBy { get; set; }
public Boolean Active { get; set; }
public virtual ICollection<Tbl_Book> Tbl_Books { get; set; }
}
}
查看:
@model IEnumerable<Library3.DAL.PrmTbl_Level>
@using (Html.BeginForm("Update", "ReaderLevel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<table>
<thead>
<tr>
<th> @Html.DisplayNameFor(model => model.LevelID) </th>
<th> @Html.DisplayNameFor(model => model.LevelDesc) </th>
<th> @Html.DisplayNameFor(model => model.EnteredBy) </th>
<th> @Html.DisplayNameFor(model => model.Active) </th>
</tr>
</thead>
@foreach (var item in Model) {
var thisID = item.LevelID;
var thisDesc = item.LevelDesc;
var thisEnteredBy = item.EnteredBy;
var thisActive = item.Active;
<tr>
<td class="tdsm centered fade"> @thisID </td>
<td> <input type="text" name="@(thisID + ".desc")" value="@thisDesc" /> </td>
<td class="tdmed centered fade"> @thisEnteredBy </td>
<td class="tdsm centered">
@{
if(@thisActive) {
<text><input type="checkbox" name="@(thisID + ".active")" value="true" checked="checked" /></text>
}
else {
<text><input type="checkbox" name="@(thisID + ".active")" value="true" /></text>
}
}
</td>
</tr>
}all of this
</table>
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
}
Controller :
namespace Library3.Controllers {
public class ReaderLevelController : Controller {
private NSSchoolLibraryEntities db = new NSSchoolLibraryEntities();
public ActionResult Index() {
ViewBag.Title = "Reader Levels";
return View(db.PrmTbl_Levels.ToList());
}
[HttpPost, ActionName("Update")]
[ValidateAntiForgeryToken]
public ActionResult Update(FormCollection MyArray) {
(... Process UPDATE submission ...)
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
}
}
现在我想在我的列表顶部放置一个空字段,这样就可以创建新条目。这基本上意味着一个 View 文件现在将同时满足创建和更新功能。我插入了以下 HTML:
@using (Html.BeginForm("New", "ReaderLevel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<table>
<tr>
<td class="tdsm centered fade">
@Html.EditorFor(model => model.LevelID)
@Html.ValidationMessageFor(model => model.LevelID)</td>
<td><input type="text" name="desc" />
@Html.EditorFor(model => model.LevelDesc)
@Html.ValidationMessageFor(model => model.LevelDesc)</td>
<td class="tdmed centered fade">
@Html.EditorFor(model => model.EnteredBy)
@Html.ValidationMessageFor(model => model.EnteredBy)</td>
<td class="tdsm centered">
@Html.EditorFor(model => model.Active)
@Html.ValidationMessageFor(model => model.Active)</td>
</tr>
</table>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
...这里引用的模型的问题应该是针对 PrmTbl_Level 类的新实例 - 不使用现有的 IEnumberable 从表中吐出现有数据(至少这就是我认为的问题所在! ).
默认的单独 Create View 通过 @model
(无 IEnumberable)引用命名空间,数据作为单个对象返回(public ActionResult Create(PrmTbl_Level prmtbl_level)
) 然后毫不费力地保存到数据库 (db.PrmTbl_Levels.Add(prmtbl_level);
) - 我如何使用一个 @model
语句实现相同的目的? (ELI5)。非常感谢所有帮助 - 无论是在实践上还是在所涉及的概念上!
最佳答案
简而言之,不要使用 IEnumerable<Library3.DAL.PrmTbl_Level>
对于您的模型,您应该创建一个自定义类来表示列表(以显示和编辑当前的方式)以及附加的 PrmTbl_Level
您可以填充以创建新的实体/行。
所以,像这样:
public class MyCustomModel
{
public MyCustomModel()
{
MyList = new List<PrmTbl_Level>();
MyItemToCreate = new PrmTbl_Level();
}
public List<PrmTbl_Level> MyList { get; set; }
public PrmTbl_Level MyItemToCreate { get; set; }
}
现在您的强类型模型是 MyCustomModel,您需要更改 View 以正确引用新模型(例如 model => model.LevelID
变为 model => model.MyList.LevelID
)。
不是一个完整的解决方案,但应该可以帮助您入门。
关于c# - VS 2012 MVC - 对更新和创建功能使用一个 View 和模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18393236/