Asp.Net MVC 方法

Asp.Net MVC Methods

我无法通过这种方式创建和更新信息:使用 Initializer,并组合两个模型 类 以获得索引视图,我有两个表 EmployeeInformation 和 DesignationHierarchy 为:

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
)

模型类为:

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);
}

和观看次数:

<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>

我认为问题可能出在您的模型 classes 中,因为您使用 decimal 作为主键。请尝试修改您的模型如下:

public class DesignationHierarchy
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public decimal Id { get; set; }
    public decimal EmployeeId { get; set; }
    public decimal LineManagerId { get; set; }

}
public class EmployeeInformation
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public decimal Id { get; set; }
    public string Name { get; set; }

}

注意:不要忘记在文件顶部添加 using System.ComponentModel.DataAnnotations; 它是 [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 所在的命名空间。

更新:

你写道你使用代码优先,所以你必须有这样的上下文 class:

 public class MyContext : DbContext 
  { 
    public DbSet<DesignationHierarchy> DesignationHierarchys { get; set; } 
    public DbSet<EmployeeInformation> EmployeeInformations{ get; set; } 
  } 

那么您可以尝试使用此代码创建和编辑操作吗?

这是 Create 的代码:

public ActionResult Create()
{
    ViewBag.EmployeeName = new SelectList(db.EmployeeInformation.ToList(),"Id","Name");

    return View();
} 

[HttpPost]
public ActionResult Create(DesignationHierarchy designationHierarchy)
{
    if (ModelState.IsValid)
    {
       using (var db = new MyContext ()) 
         { 
              db.DesignationHierarchy.Add(designationHierarchy); 
              db.SaveChanges(); 
         } 
         return RedirectToAction("Index"); 
    }
      View(designationHierarchy); 
 }

对于Edit

public ActionResult Edit(decimal id)
{
    ViewBag.EmployeeName = new SelectList(db.EmployeeInformation.ToList(), "Id", "Name",designationHierarchy);

     using (var db = new MyContext()) 
     { 
            return View(db.DesignationHierarchy.Find(id)); 
     } 
}

[HttpPost]
public ActionResult Edit(DesignationHierarchy designationHierarchy)
{
    if (ModelState.IsValid)
    {
      using (var db = new MyContext()) 
       { 
        db.Entry(designationHierarchy).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
       }
    }
    return View(designationHierarchy);
}

更新 2:这真的很奇怪。我仍然认为这可能是因为 decimal 值。

能否请你更换

DesignationHierarchy designationHierarchy = db.DesignationHierarchy.Find(id);

在您的编辑操作中

 DesignationHierarchy designationHierarchy = db.DesignationHierarchy
                                               .SingleOrDefault(x=> x.Id == id);

通过将 Id 的数据类型更改为 int,并在视图中进行更改,即在编辑视图中,使用 HTML 辅助方法将 Id 保持隐藏状态。我仍然无法生成索引视图。尽管我上述问题的剩余答案是:

数据库脚本:

    CREATE TABLE [dbo].[DesignationHierarchy](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [EmployeeId] [numeric](18, 0) NULL,
      [LineManagerId][numeric](18, 0) NULL
    )

型号Class:

    public class DesignationHierarchy
        {
            [Key]
            public int Id { get; set; }
            public decimal EmployeeId { get; set; }
            public decimal LineManagerId { get; set; }

        }

操作方法:

public ActionResult Create()
{
    ViewBag.EmployeeName = new SelectList(db.EmployeeInformation.ToList(), "Id", "Name");
    ViewBag.LineManagerName = 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");
    ViewBag.LineManagerName = new SelectList(db.EmployeeInformation.ToList(), "Id", "Name");

    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);
}

和要创建的视图:

 <div class="editor-label left_align">
            @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.LineManagerName, "--Select Name--")
            @Html.ValidationMessageFor(model => model.LineManagerId)
        </div>

并编辑:

    <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.LineManagerName, "--Select Name--")
        @Html.ValidationMessageFor(model => model.LineManagerId)
    </div>