Я не могу создавать и обновлять информацию по этому пути: Использование инициализатора и Расчесывание две модели классов, чтобы получить индекс представления, у меня есть две таблицы EmployeeInformation & DesignationHierarchy как:Asp.Net MVC Методы
CREATE TABLE [dbo].[DesignationHierarchy](
[Id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[EmployeeId] [numeric](18, 0) NULL,
[LineManagerId][numeric](18, 0) NULL
)
&
CREATE TABLE [dbo].[EmployeeInformation](
[Id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](255) NULL
)
и ModelClasses как:
public class DesignationHierarchy
{
public decimal Id { get; set; }
public decimal EmployeeId { get; set; }
public decimal LineManagerId { get; set; }
}
public class EmployeeInformation
{
public decimal Id { get; set; }
public string Name { get; set; }
}
и
public class EmployeeInformationHierarchy
{
public EmployeeInformation EmployeeInformation { get; set; }
public DesignationHierarchy DesignationHierarchy { get; set; }
}
и методы создания:
public ActionResult Create()
{
ViewBag.EmployeeName = new SelectList(db.EmployeeInformation.ToList(),"Id","Name");
return View();
}
[HttpPost]
public ActionResult Create(DesignationHierarchy designationHierarchy)
{
if (ModelState.IsValid)
{
db.DesignationHierarchy.Add(designationHierarchy);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(designationHierarchy);
}
и редактировать:
public ActionResult Edit(decimal id)
{
DesignationHierarchy designationHierarchy = db.DesignationHierarchy.Find(id);
ViewBag.EmployeeName = new SelectList(db.EmployeeInformation.ToList(), "Id", "Name",designationHierarchy);
return View(designationHierarchy);
}
[HttpPost]
public ActionResult Edit(DesignationHierarchy designationHierarchy)
{
if (ModelState.IsValid)
{
db.Entry(designationHierarchy).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(designationHierarchy);
}
и индекс:
public ViewResult Index()
{
var designationHierarchy = from d in db.EmployeeInformation
join r in db.DesignationHierarchy on d.Id equals r.LineManagerId
select new EmployeeInformationHierarchy { EmployeeInformation = d, DesignationHierarchy = r };
return View(designationHierarchy);
}
и Views:
<div class="editor-label left_align">
@Html.HiddenFor(model => model.Id)
@Html.LabelFor(model => model.EmployeeId,"Name")
</div>
<div class="editor-field left_right_padding">
@Html.DropDownListFor(m => m.EmployeeId, (SelectList)ViewBag.EmployeeName, "--Select Name--")
@Html.ValidationMessageFor(model => model.EmployeeId)
</div>
<div class="editor-label left_align">
@Html.LabelFor(model => model.LineManagerId,"Line Manager")
</div>
<div class="editor-field left_right_padding">
@Html.DropDownListFor(m => m.LineManagerId, (SelectList)ViewBag.EmployeeName, "--Select Name--")
@Html.ValidationMessageFor(model => model.LineManagerId)
</div>
и вид на индекс:
<td>
@Html.DisplayFor(modelItem => item.EmployeeInformation.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmployeeInformation.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.DesignationHierarchy.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.DesignationHierarchy.Id })
</td>
Так что это проблема? –
@StephenMuecke При создании отношения между сотрудниками значение не обновляется в таблице базы данных ОбозначениеИерархия. Не могу найти, что не так с моим кодом. –
Сначала вы используете Code First или DataBase? – user2771704