[英]Asp.Net MVC Methods
I can't create and update the information by this way: Using Initializer, and Combing two models classes to get the Index View,I have two tables EmployeeInformation & DesignationHierarchy as: 我无法通过这种方式创建和更新信息:使用Initializer和Combin两个模型类以获取索引视图,我有两个表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
)
and ModelClasses as: 和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; }
}
and 和
public class EmployeeInformationHierarchy
{
public EmployeeInformation EmployeeInformation { get; set; }
public DesignationHierarchy DesignationHierarchy { get; set; }
}
and the methods to Create: 以及创建方法:
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);
}
and edit: 并编辑:
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);
}
and for index: 对于索引:
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);
}
and 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>
and view for index: 并查看索引:
<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>
I think the problem could be in your model classes, because you use decimal
as primary key. 我认为问题可能出在您的模型类中,因为您使用
decimal
作为主键。 Please try to modify your models as below: 请尝试如下修改您的模型:
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; }
}
Note : don't forget to add using System.ComponentModel.DataAnnotations;
注意 :不要忘记
using System.ComponentModel.DataAnnotations;
添加using System.ComponentModel.DataAnnotations;
at the top of your file it's namespace where [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
located. 在文件顶部,它是
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
所在的名称空间。
UPDATE: 更新:
You wrote that you use Code First, so you have to had context class like this: 您写道,您使用代码优先,因此必须具有如下上下文类:
public class MyContext : DbContext
{
public DbSet<DesignationHierarchy> DesignationHierarchys { get; set; }
public DbSet<EmployeeInformation> EmployeeInformations{ get; set; }
}
So could you try this code for you Create and Edit actions? 那么,您可以为创建和编辑操作尝试使用此代码吗?
Here is code for Create
: 这是
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);
}
And for Edit
: 对于
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);
}
UPDATE 2 : That's really strange. 更新2 :真的很奇怪。 I still think that it could be because of
decimal
value. 我仍然认为可能是因为
decimal
值。
Could you please replace 你能代替吗
DesignationHierarchy designationHierarchy = db.DesignationHierarchy.Find(id);
in your Edit Action with 在您的“编辑操作”中
DesignationHierarchy designationHierarchy = db.DesignationHierarchy
.SingleOrDefault(x=> x.Id == id);
By Changing the datatype of Id to int,and changes in the Views ie in Edit view the Id is kept Hidden using HTML Helper Methods. 通过将Id的数据类型更改为int,并在视图中进行更改,即在“编辑”视图中,可以使用HTML Helper Methods将ID保持隐藏状态。 Still I am having problem to generate a Index View.
我仍然在生成索引视图时遇到问题。 Though the Remaining answer for my above question is:
尽管上述问题的剩余答案是:
DB Script: 数据库脚本:
CREATE TABLE [dbo].[DesignationHierarchy](
[Id] [int] IDENTITY(1,1) NOT NULL,
[EmployeeId] [numeric](18, 0) NULL,
[LineManagerId][numeric](18, 0) NULL
)
Model Class: 型号类别:
public class DesignationHierarchy
{
[Key]
public int Id { get; set; }
public decimal EmployeeId { get; set; }
public decimal LineManagerId { get; set; }
}
Action Methods: 动作方法:
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);
}
and Views to Create: 和要创建的视图:
<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>
and Edit: 并编辑:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.