ASP.NET MVC 中将FormCollection与实体间转换方法 (转)

将Action动作中传递的FormCollection转变成对应的实体,可以使用Controller的TryUpdateModel()方法。

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

也可以直接使用实体作为参数:

public ActionResult Create([Bind(Include = "Id,Code,Name")] Student studentModel)
上一篇:python的Web框架:Django路由系统以及模板导入


下一篇:不会难道我还不能附上链接吗