简体   繁体   中英

ASP.NET MVC 4: How to update model from view on submit form

I create some form and I need to assign field CreationDate on submit form:

@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" />
}

How to do that?

You have to do this in the controller action before passing it to data access like:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM