在视图创建和编辑(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")
}
...
在下拉列表中列出右侧的元素。 请帮助我们使用这些领域的模型:)
答案 0 :(得分: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)