Reputation: 177
Field is not created or edited in views Create and Edit (C# MVC4 CodeFirst). The rest of the field with the data types string or integer created and edited properly and correctly.
In Model Requirement:
...
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; }
...
Controller RequirementController (in method 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();
}
View Create:
...
@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")
}
...
In the drop-down lists the elements of the right. Help please with the work with these fields model:)
Upvotes: 0
Views: 99
Reputation:
Your properties CreatedBy
, Responsible
and InterestedPersons
are complex objects. A <select>
only posts back a single value (or array of values in the case of multiple
). In addition the name of your dropdowns are all UsersId
but your model does not contain a property named UsersId
so nothing is bound.
Create a view model to represent the data you want to edit.
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; }
}
Controller
public ActionResult Create()
{
RequirementVM model = new MyViewModel();
model.UserList = new SelectList(db.Users, "UsersId", "Surname");
return View(model);
}
[HttpPost]
public ActionResult Create(RequirementVM model)
{
...
}
View
@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)
Upvotes: 1