62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace DataEntity
{
[Table("Hr_Department")]
public class Department
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DeptID { get; set; }
[Display(Name = "部门名称")]
[StringLength(100)]
[Required(ErrorMessage = "部门名称不能为空")]
public string DeptName { get; set; }
[Display(Name = "上级部门")]
public int? ParentID { get; set; }
[Display(Name = "部门编号")]
[StringLength(100)]
[Required(ErrorMessage = "部门编号不能为空")]
public string DeptCode { get; set; }
[Display(Name = "电话")]
[StringLength(100)]
public string DeptPhone { get; set; }
[Display(Name = "传真")]
[StringLength(100)]
public string DeptFax { get; set; }
}
}
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "deptForm" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend id="legendText">编辑部门</legend>
<div id="keyField">
@Html.HiddenFor(model => model.DeptID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DeptName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DeptName)
@Html.ValidationMessageFor(model => model.DeptName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DeptCode)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DeptCode)
@Html.ValidationMessageFor(model => model.DeptCode)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DeptPhone)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DeptPhone)
@Html.ValidationMessageFor(model => model.DeptPhone)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DeptFax)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DeptFax)
@Html.ValidationMessageFor(model => model.DeptFax)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ParentID)
</div>
<div class="editor-field">
<input id="ParentID" name="ParentID" value="@ParentID" required="true" />
</div>
<p>
<input type="button" value="保存" onclick="saveDeptForm()" />
</p>
</fieldset>
}
/// <summary>
/// 新建部门 父级部门ID传入
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Create(int id)
{
ViewBag.EditMode = "Create";
ViewBag.DeptID = id;//这是父级部门ID 与新的部门ID无关
return View("Edit");
}
<div id="keyField">
@Html.HiddenFor(model => model.ID)
</div>
function saveDeptForm() {
alert($("#deptForm").serialize());
if ($('#deptForm').valid()) {
$.post(actionUrl, $("#deptForm").serialize(), function (result) {
if (result.success) {
$.messager.alert('提示', '数据已保存!');
$('#deptTree').tree("reload"); //刷新树
$('#cc').panel('refresh', '/Department/Grid/' + $("#ParentID").combotree("getValue")); //显示grid
} else {
$.messager.alert('提示', '数据保存出现错误!', 'error');
}
}, 'json');
}
else {
alert("验证不通过");
}
}
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
/// <summary>
/// 插入单个实体
/// </summary>
/// <param name="entity"></param>
public void Insert(TEntity entity)
{
dbSet.Add(entity);
SaveChanges();
}
private DataContext db = new DataContext();
private EntityRepository<Department> DeptRepository;
public DepartmentController()
{
DeptRepository = new EntityRepository<Department>(db);
}