Why in ASP MVC C# not instantiate a class when a record is added?

There is a code
public ActionResult Create()
{
 return View();
}

[HttpPost]
public ActionResult Create(Movie newMovie)
{

 if (ModelState.IsValid)
{
db.AddToMovies(newMovie);
db.SaveChanges();

 return RedirectToAction("Index");
}
else
{
 return View(newMovie);
}
}


Why not create a class instance, like for example here:
1) https://professorweb.ru/my/ASP_NET/mvc/level7/7_1.php Binding complex types
using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.Mvc;
using MvcModels.Models;

namespace MvcModels.Controllers
{
 public class HomeController : Controller
{
 // ...

 public ActionResult CreateUser()
{
 return View(new User());
}

[HttpPost]
 public ActionResult CreateUser(User model)
{
 return View("Index", model);
}
}
}


2) in php(yii2), for example
<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\EntryForm;

class SiteController extends Controller
{
 // ...existing code...

 public function actionEntry()
{
 $model = new EntryForm();

 if ($model->load(Yii::$app->request->post()) && $model->validate()) {
 // valid data received in $model

 // do something meaningful here about $model ...

 return $this->render('entry-confirm', ['model' => $model]);
 } else {
 // either the page is initially displayed or there is some validation error
 return $this->render('entry', ['model' => $model]);
}
}
}
June 14th 19 at 18:18
1 answer
June 14th 19 at 18:20
Solution
public ActionResult Create()
{
 // Create a new instance useless, because we immediately go into the render
 return View();
}

[HttpPost]
// New instance of class Movie will be created before calling this method automatically
public ActionResult Create(Movie newMovie)
{
 // Here you will find out if all fields are correctly sampley
 if (ModelState.IsValid)
{
db.AddToMovies(newMovie);
db.SaveChanges();
 // If everything is correct go ahead
 return RedirectToAction("Index");
}
else
{
 // If there are errors, then open again, but with predydushimi data
 return View(newMovie);
}
}
And how then
public ActionResult Create()
{
 // Create a new instance useless, because we immediately go into the render
 return View();
}


attached to the annotation model, without creating instance?

@model AccountRegisterViewModel
<h2>
Register</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<label>
Name
 @Html.TextBoxFor(m => m.Form.Name)
</label>
<label>
State
 @Html.DropDownListFor(m => m.Form.State, Model.StateSelectList)
</label>
 <input type="submit" value="Register me">
}
- quincy.Lynch commented on June 14th 19 at 18:23
rather it creates an empty model - Josef commented on June 14th 19 at 18:26
Sori and how it is created empty? Before an instance of all created, and there is no need to create. - quincy.Lynch commented on June 14th 19 at 18:29
somewhere in there:
Activator.CreateInstance(modelType)
where modelType is the type you specified in *.using cshtml @model - buford_Hand41 commented on June 14th 19 at 18:32

Find more questions by tags ASP.NETC#