字段未创建或编辑
本文关键字:编辑 创建 字段 | 更新日期: 2023-09-27 18:26:24
在视图创建和编辑(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; }
...
控制器需求控制器(在方法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")
}
...
在下拉列表中列出右侧的元素。请帮助使用这些字段模型:)
您的属性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)