您的位置:首页 > 编程语言 > ASP

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");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: