ASP.NET MVC 中将FormCollection与实体间转换方法
2016-01-18 14:38
543 查看
http://blog.csdn.net/lutinghuan/article/details/8449296
将Action动作中传递的FormCollection转变成对应的实体,可以使用Controller的TryUpdateModel()方法。
示例如下:
[csharp] view plaincopy
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
if (ModelState.IsValid)
{
var student = new Student();
//在这里转换
TryUpdateModel<Student>(student, collection);
dalStudent.Add(student);
return RedirectToAction("Index");
}
else
return View();
}
catch
{
return View("Create");
}
}
将Action动作中传递的FormCollection转变成对应的实体,可以使用Controller的TryUpdateModel()方法。
示例如下:
[csharp] view plaincopy
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
if (ModelState.IsValid)
{
var student = new Student();
//在这里转换
TryUpdateModel<Student>(student, collection);
dalStudent.Add(student);
return RedirectToAction("Index");
}
else
return View();
}
catch
{
return View("Create");
}
}
相关文章推荐
- 【深入ASP.NET原理系列】--ASP.NET页面生命周期
- ASP.Net MVC 之FileResult
- ASP.NET Web API中实现版本的几种方式
- spring中AspectJ(16)
- ASP.NET MVC 3 常用
- repeater中CheckBoxList/RadioButtonList取值赋值
- ASP.NET WebAPI Get使用复杂型别
- ASP.Net MVC的ViewBag一个坑,不要跳进去
- ASP.Net MVC的ViewBag一个坑,不要跳进去
- asp gridview 统计总条数
- 在ASP.NET中基于Owin OAuth使用Client Credentials Grant授权发放Token
- asp.net cache 缓存
- ASP.NET Core 1.0基础之应用启动
- [转]Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱
- Razor:从aspx到cshtml常见错误及正确书写方法
- javaEE之-------Spring中的aspectJ的应用
- iReport与JasperReport PDF中文解决
- asp.net实现负载均衡
- spring (四) aop/aspectj
- ASPNET5 诊断