public class FormViewModel { public string Name { get; set; } public string Email { get; set; } public int Age { get; set; } }
@model FormViewModel
[HttpPost] public IActionResult Submit(FormViewModel model) { if (ModelState.IsValid) { var entity = new FormDataEntity { Name = model.Name, Email = model.Email, Age = model.Age };
//将表单数据存储到数据库
_context.FormData.Add(entity);
_context.SaveChanges();
return RedirectToAction("Success");
}
return View(model); }
在这个示例中,我们首先检查模型状态是否有效,然后创建一个新的数据实体并将表单数据存储到数据库中。最后,我们将用户重定向到成功页面。