Ajax 按需 提交指定字段 以 Json Model 形式 post 到 action
2011-06-03 14:37
351 查看
Action 如下,其中传递过来的master已经从json model 转换为 实体Model
[HttpPost]
public ActionResult Edit(Master master)
{
if (ModelState.IsValid)
{
db.Master.Attach(master);//附加到DBContext
SetModifiedPropertyByNeed<Master>(master);//把前天传递过来的JsonModel字段都标记为更新
db.SaveChanges();
return RedirectToAction("Index");
}
return View(master);
}
利用反射,查找Model中 传递过来的指定字段,并将其 stateEntry 设置为 已更新。
public void SetModifiedPropertyByNeed<T>(T t)
{
if (t == null)
{
return;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties();
if (properties.Length <= 0)
{
return;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name; //名称
object value = item.GetValue(t, null); //值
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
if (value != null)
{//值不为null的都是需要更新的字段。
var stateEntry = ((IObjectContextAdapter)db).ObjectContext
.ObjectStateManager.GetObjectStateEntry(t);
stateEntry.SetModifiedProperty(name);
}
}
else
{
SetModifiedPropertyByNeed(value);
}
}
}
参考文献:
1 、dudu的 新问题新方法:在Entity Framework中实现指定字段更新
[HttpPost]
public ActionResult Edit(Master master)
{
if (ModelState.IsValid)
{
db.Master.Attach(master);//附加到DBContext
SetModifiedPropertyByNeed<Master>(master);//把前天传递过来的JsonModel字段都标记为更新
db.SaveChanges();
return RedirectToAction("Index");
}
return View(master);
}
利用反射,查找Model中 传递过来的指定字段,并将其 stateEntry 设置为 已更新。
public void SetModifiedPropertyByNeed<T>(T t)
{
if (t == null)
{
return;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties();
if (properties.Length <= 0)
{
return;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name; //名称
object value = item.GetValue(t, null); //值
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
if (value != null)
{//值不为null的都是需要更新的字段。
var stateEntry = ((IObjectContextAdapter)db).ObjectContext
.ObjectStateManager.GetObjectStateEntry(t);
stateEntry.SetModifiedProperty(name);
}
}
else
{
SetModifiedPropertyByNeed(value);
}
}
}
参考文献:
1 、dudu的 新问题新方法:在Entity Framework中实现指定字段更新
相关文章推荐
- ajax(第一篇)将json数据提交到action并解析
- 通过Ajax进行Post提交Json数据的方法
- Ajax提交Json字符串,.net反序列化成Model对象
- JS 提交表单2-Query Ajax post(json数组,form表单经serialize()序列化,html拼接)
- jquery ajax post 方式 data中以json形式并且定义的是拼接好的字符串变量
- ajax 提交post get 数据,得到json结果解析
- ajax使用POST提交json数据
- IE8下struts2 采用ajax提交表单,返回json字符串,解析不了,直接保存json字符串。提示下载action.
- How to use JSon data in mvc action and post form data use JQuery ajax
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- Ajax进行POST提交,Spring MVC整合fastJson
- 初涉Ajax,以post或get方法发送数据,以json或xml形式接收服务器返回的请求
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- $.ajax Post 数据指定类型为application/json 的时候,后台无法直接request到数据
- AJAX如何以POST的方式提交JSON数据到Form
- ajax指定为post但是未设置content-type时如何获取提交的内容
- MVC中使用Ajax提交数据 Jquery Ajax方法传值到action
- 深入理解JQuery中$.get()、$.post()、$.getJSON()、$.ajax()方法
- 深入理解JQuery中$.get()、$.post()、$.getJSON()、$.ajax()方法