There are two methods for editing
The first transmits the values for editing, and the second edits
public ActionResult EditMagazine(int id) { var editMagazineModel = new EditMagazineViewModel() { Magazine = magazineService.EditMagazine(id) }; return View(editMagazineModel); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult EditMagazine1(EditMagazineViewModel magazine) { var magazineModel = new Magazine { MagazineId = magazine.Magazine.MagazineId, Name = magazine.Magazine.Name, Number = magazine.Magazine.Number, YearOfPublishing = magazine.Magazine.YearOfPublishing }; magazineService.EditMagazine1(magazineModel); return RedirectToAction("Magazine"); } due to the fact that I'm doing the ViewModel, I had to deal with the mapping,
So the problem is that in the second method there is an exception because of this.
var magazineModel = new Magazine { I create a new object
Who will tell you how to correctly transfer the values from the first method to the second method
[NullReferenceException: Object reference not set to an instance of an object.] Here is the ViewModel
public class EditMagazineViewModel { public Magazine Magazine { get; set; } } but Model
public class Magazine { public int MagazineId { get; set; } public string Name { get; set; } public int Number { get; set; } public int YearOfPublishing { get; set; } } Here is View EditMagazine
@model Library.ViewModel.MagazineViewModel.EditMagazineViewModel @{ Layout = "~/Views/Home/Layout.cshtml"; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <link rel="stylesheet" href="~/Content/style-form.css" type="text/css"> <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300" type="text/css"> </head> <body> @using (Html.BeginForm("EditMagazine1", "Magazine")) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Magazine</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.Magazine.MagazineId) <div class="form-group"> @Html.LabelFor(model => model.Magazine.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Magazine.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Magazine.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Magazine.Number, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Magazine.Number, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Magazine.Number, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Magazine.YearOfPublishing, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Magazine.YearOfPublishing, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Magazine.YearOfPublishing, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div> </div> } 
Magazine- BulsonEditMagazineViewModel magazine- Bulson does not come empty.