ASP.NET MVC 4:如何从提交表单上的视图更新模型

时间:2016-02-10 17:01:31

标签: c# asp.net asp.net-mvc forms asp.net-mvc-4

我创建了一些表单,我需要在提交表单上指定字段CreationDate

@model Namespaces.Chapter

@using (Html.BeginForm ()) {
    <p>Title: @Html.TextBoxFor(c => c.Title)</p>
    ...
    @{ Model.CreationDate = DateTime.Now;} <- somehow like that, but this code does not works
    <input type="submit" value="Submit" />
}

怎么做?

1 个答案:

答案 0 :(得分:3)

在将控制器操作传递给数据访问之前,您必须在控制器操作中执行此操作,如:

public ActionResult SomeAction(SomeClass obj)
{
   obj.CreationDate = DateTime.Now;

   // your business logic goes here
   ................
   ................
}