Есть два метода для редактирования
Первый передает значения для редактирования, а второй редактирует
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");
}
из-за того что делаю ViewModel пришлось столкнуться с mapping'ом ,
так вот проблема в том что во втором методе выходит исключение из-за этого
var magazineModel = new Magazine
{
я создаю новый обьект
Кто подскажет как правильно передать значения с первого метода для второго метода
[NullReferenceException: Object reference not set to an instance of an object.]
Вот ViewModel
public class EditMagazineViewModel
{
public Magazine Magazine { get; set; }
}
а вот Model
public class Magazine
{
public int MagazineId { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public int YearOfPublishing { get; set; }
}
Вот 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
EditMagazineViewModel magazine