未创建或编辑字段
Posted
技术标签:
【中文标题】未创建或编辑字段【英文标题】:field is not created or edited 【发布时间】:2014-10-14 12:28:34 【问题描述】:未在视图 Create and Edit (C# MVC4 CodeFirst) 中创建或编辑字段。 正确和正确地创建和编辑数据类型字符串或整数的字段的其余部分。
在模型中要求:
...
public int RequirementId get; set;
//[StringLength(500, MinimumLength = 500)]
public string Definition get; set;
public string Rationale get; set;
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "0:yyyy-MM-dd", ApplyFormatInEditMode = true)]
public DateTime CreatedOn get; set;
public virtual Users CreatedBy get; set;
public virtual Users Responsible get; set;
public virtual ICollection<Users> InterestedPersons get; set;
public string CurentVersion get; set;
public StateEnum State get; set;
public PriorityEnum Priority get; set;
public StabilityEnum Stability get; set;
public TypeEnum Type get; set;
public virtual BusinessRule Source get; set;
public virtual ICollection<TestCase> TestCase get; set;
public string UserPart get; set;
...
控制器 RequirementController(在 Create 方法中):
public ActionResult Create()
RequirementViewModel reqVM = new RequirementViewModel();
AutoMapper.Mapper.CreateMap<RequirementViewModel, Requirement>();
reqVM.UserList = new SelectList(db.Users, "UsersId", "Surname");
reqVM.BusinessRuleList = new SelectList(db.BusinessRule, "BusinessRuleId", "Definition");
reqVM.TestCaseList = new SelectList(db.TestCase, "TestCaseId", "Title");
Requirement requirement = AutoMapper.Mapper.Map<RequirementViewModel, Requirement>(reqVM);
return View();
//
// POST: /Requirement/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Requirement requirement)
try
AutoMapper.Mapper.CreateMap<RequirementViewModel, Requirement>();
reqVM.UserList = new SelectList(db.Users, "UsersId", "Surname");
reqVM.BusinessRuleList = new SelectList(db.BusinessRule, "BusinessRuleId", "Definition");
reqVM.TestCaseList = new SelectList(db.TestCase, "TestCaseId", "Title");
Requirement requirement = AutoMapper.Mapper.Map<RequirementViewModel, Requirement>(reqVM);
if (ModelState.IsValid)
db.Requirement.Add(requirement);
db.SaveChanges();
return RedirectToAction("Index"); //, new id = requirement.InterestedPersons
catch (DataException /* dex */)
//Log the error (uncomment dex variable name after DataException and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
return View();
查看创建:
...
@model OpenSoft.AgileAnalytics.EF.Models.Requirement
@
ViewBag.Title = "Create";
<h2>Create</h2>
@using (html.BeginForm())
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Requirement</legend>
@*@Html.EditorForModel()*@
<div class="editor-label">
@Html.LabelFor(model => model.Definition)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Definition)
@Html.ValidationMessageFor(model => model.Definition)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rationale)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rationale)
@Html.ValidationMessageFor(model => model.Rationale)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CreatedOn)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CreatedOn)
@Html.ValidationMessageFor(model => model.CreatedOn)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CreatedBy)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CreatedBy, Model.UserList, "-Select-") //error
@Html.ValidationMessageFor(model => model.CreatedBy)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Responsible)
</div>
<div class="editor-field">
@Html.DropDownListFor(model=>model.Responsible, Model.UserList, "-Select-") //error
@Html.ValidationMessageFor(model => model.Responsible)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.InterestedPersons)
</div>
<div class="editor-field">
@Html.ListBoxFor(model=>model.InterestedPersons, Model.UserList) //error
@Html.ValidationMessageFor(model => model.InterestedPersons)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CurentVersion)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CurentVersion)
@Html.ValidationMessageFor(model => model.CurentVersion)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserPart)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserPart)
@Html.ValidationMessageFor(model => model.UserPart)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
@*@Html.EnumDropDownList(model => model.StateEnum)*@
@Html.DropDownListFor(m => m.State, new SelectList(Enum.GetValues(typeof(OpenSoft.AgileAnalytics.EF.Models.StateEnum))))
@Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Priority)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.Priority, new SelectList(Enum.GetValues(typeof(OpenSoft.AgileAnalytics.EF.Models.PriorityEnum))))
@Html.ValidationMessageFor(model => model.Priority)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Stability)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.Stability, new SelectList(Enum.GetValues(typeof(OpenSoft.AgileAnalytics.EF.Models.StabilityEnum))))
@Html.ValidationMessageFor(model => model.Stability)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Type)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.Type, new SelectList(Enum.GetValues(typeof(OpenSoft.AgileAnalytics.EF.Models.TypeEnum))))
@Html.ValidationMessageFor(model => model.Type)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Source)
</div>
<div class="editor-field">
@Html.DropDownList("BusinessRuleId", "-Select-")
@Html.ValidationMessageFor(model => model.Source)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TestCase)
</div>
<div class="editor-field">
@Html.DropDownList("TestCaseId", "-Select-")
@Html.ValidationMessageFor(model => model.TestCase)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts
@Scripts.Render("~/bundles/jqueryval")
...
在下拉列表右边的元素。 请帮助处理这些字段模型的工作:)
【问题讨论】:
【参考方案1】:您的属性CreatedBy
、Responsible
和InterestedPersons
是复杂对象。 <select>
仅回发单个值(或 multiple
的情况下的值数组)。此外,您的下拉列表的名称都是 UsersId
,但您的模型不包含名为 UsersId
的属性,因此没有任何绑定。
创建一个视图模型来表示您要编辑的数据。
public class RequirementVM
public int RequirementId get; set;
public string Definition get; set;
.... // other properties of Requirement that you want to edit
[Required]
[Display(Name="Created by")]
public int? CreatedBy get; set;
[Required]
public int? Responsible get; set;
public int[] InterestedPersons get; set;
public SelectList UserList get; set;
控制器
public ActionResult Create()
RequirementVM model = new MyViewModel();
model.UserList = new SelectList(db.Users, "UsersId", "Surname");
return View(model);
[HttpPost]
public ActionResult Create(RequirementVM model)
...
查看
@model RequirementVM
....
@Html.LabelFor(model => model.Definition)
@Html.EditorFor(model => model.Definition)
@Html.ValidationMessageFor(model => model.Definition)
... other properties of RequirementVM to edit
@Html.LabelFor(m => m.CreatedBy)
@Html.DropDownListFor(m => m.CreatedBy, Model.UserList, "-Select-")
@Html.ValidationMessageFor(m => m.CreatedBy)
@Html.LabelFor(m => m.Responsible)
@Html.DropDownListFor(m => m.Responsible, Model.UserList, "-Select-")
@Html.ValidationMessageFor(m => m.Responsible)
@Html.LabelFor(m => m.InterestedPersons)
@Html.ListBoxFor(m => m.InterestedPersons, Model.UserList)
@Html.ValidationMessageFor(m => m.InterestedPersons)
【讨论】:
不幸的是我不能按照你的建议去做。毕竟,事实证明我将拥有这三个字段的单一视图,并且我需要将它们与同一页面上另一个模型的其他字段放在一起。 我不知道你的其他属性是什么 - 你没有在你的问题中发布它们。视图模型显然也需要包含其他属性! 我加了更详细的代码,请大家帮忙理解 @ТатьянаДрузенко,我已经更新了我的答案,从您的数据模型中添加了一些额外的属性。您只需在RequirementVM
中包含所有要编辑的属性,并根据RequirementVM
(而不是Requirement
)查看。注意Source
和TestCase
需要类似于我为CreatedBy
和InterestedPersons
显示的内容(它们也是复杂对象)。当此帖子返回到public ActionResult Create(RequirementVM model)
时,model.CreatedBy
将包含所选用户的 ID,model.InterestedPersons
将包含所选用户 ID 的数组
,非常感谢您的帮助!我会尽力做到你说的。以上是关于未创建或编辑字段的主要内容,如果未能解决你的问题,请参考以下文章