简体   繁体   中英

Passing model from view to controller

After Learning Jon Galloways MVC Music Store Example.I Just didn't understood the create view How to pass model to controller in which we could see it from parameter in the action Create(Movie movie). Thanks.

        [HttpPost]
        public ActionResult Create(Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Movies.Add(movie);//Where is the movie come from?
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(movie);
        }

通常,您不将模型传递给控制器​​,而是在控制器中创建模型的实例。

In the code example you have posted, the Movie model will be created through model binding. During this process any of your form variables will be matched up with the object specified in the action.

For instance the value of

<input type="text" name="Title"/>

would be assigned to movie's Title property.

A view can be associated with a model by declaring (Razor syntax)

@model GallowaySample.Movie

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