ASP.NETMVC中将FormCollection与实体间转换方法
将Action动作中传递的FormCollection转变成对应的实体,可以使用Controller的TryUpdateModel()方法。
- 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");
- }
- }
|
请发表评论