Merhaba,
Resim yüklemek istediğimde bu hata ile karşılaşıyorum. Sorun nerede acaba?
"The model item passed into the ViewDataDictionary is of type 'BlogEntity.Concrete.Article', but this ViewDataDictionary instance requires a model item of type 'BlogUI.Areas.Admin.Models.ImageModel'."
Article Image için Models üzerinde yeni Class oluşturmuştum. Kodun içerisinde var.
[CODE title="c#"]
Article Class =
public class Article
{
[Key]
public int ArticleId { get; set; }
[Required(ErrorMessage ="Bu alan boş geçilmemeli.")]
[MinLength(3,ErrorMessage ="En az 2 karakter girilmeli.")]
[MaxLength(50,ErrorMessage ="En fazla 50 karakter girilmeli.")]
public string ArticleTitle { get; set; }
[MinLength(100, ErrorMessage = "En az 100 karakter girilmeli.")]
[Required(ErrorMessage = "Bu alan boş geçilmemeli.")]
public string ArticleContent { get; set; }
[NotMapped]
public string ArticleImage { get; set; }
public int ViewsCount { get; set; }
public int CommentCount { get; set; }
public DateTime ArticleCreatedDate { get; set; }
public bool ArticleStatus { get; set; }
public ICollection<Comment> Comments { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public int Id { get; set; }
public User User { get; set; }
}
Models içindeki yeni resim class'ı =
public class ImageModel
{
public string ArticleTitle { get; set; }
public string ArticleContent { get; set; }
public IFormFile ArticleImage { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public DateTime ArticleCreatedDate { get; set; }
}
Controller tarafı =
[HttpGet]
public IActionResult ImageUpload()
{
return View();
}
[HttpPost]
public IActionResult ImageUpload(ImageModel p)
{
Article a = new Article();
if (p.ArticleImage != null)
{
var extension = Path.GetExtension(p.ArticleImage.FileName);
var newimage = Guid.NewGuid() + extension;
var location = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/BlogImageFiles/", newimage);
var stream = new FileStream(location, FileMode.Create);
p.ArticleImage.CopyTo(stream);
a.ArticleImage = newimage;
}
a.ArticleStatus = true;
a.ArticleTitle = p.ArticleTitle;
a.ArticleCreatedDate = p.ArticleCreatedDate;
am.Add(a);
return RedirectToAction("Index");
}
}
View tarafı =
@model BlogUI.Areas.Admin.Models.ImageModel
@{
ViewData["Title"] = "Add";
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<br />
<h2>Yeni Blog Ekle</h2>
<br />
<form class="form-group" method="post" enctype="multipart/form-data" asp-controller="Blog" asp-action="Add">
<div class="card">
<div class="card-body">
<div class="forms-sample">
<div class="form-group">
<h4>Blog Başlık</h4>
@Html.TextBoxFor(x => x.ArticleTitle, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.ArticleTitle, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<h4>Blog İçerik</h4>
@Html.TextAreaFor(x => x.ArticleContent, 7, 3, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.ArticleContent, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<h4>Blog Kategori</h4>
@Html.DropDownListFor(x => x.CategoryId, (List<SelectListItem>)ViewBag.c, new { @class = "form-control" })
</div>
<div class="form-group">
<h4>Blog Resmi</h4>
<input type="file" name="ArticleImage" />
</div>
<button class="btn btn-info">Blog Ekle</button>
<input type="button" class="btn btn-danger" value="Geri Dön" onclick="location.href = '@Url.Action("Index")'" />
</div>
</div>
</div>
</form>
[/CODE]
Resim yüklemek istediğimde bu hata ile karşılaşıyorum. Sorun nerede acaba?
"The model item passed into the ViewDataDictionary is of type 'BlogEntity.Concrete.Article', but this ViewDataDictionary instance requires a model item of type 'BlogUI.Areas.Admin.Models.ImageModel'."
Article Image için Models üzerinde yeni Class oluşturmuştum. Kodun içerisinde var.
[CODE title="c#"]
Article Class =
public class Article
{
[Key]
public int ArticleId { get; set; }
[Required(ErrorMessage ="Bu alan boş geçilmemeli.")]
[MinLength(3,ErrorMessage ="En az 2 karakter girilmeli.")]
[MaxLength(50,ErrorMessage ="En fazla 50 karakter girilmeli.")]
public string ArticleTitle { get; set; }
[MinLength(100, ErrorMessage = "En az 100 karakter girilmeli.")]
[Required(ErrorMessage = "Bu alan boş geçilmemeli.")]
public string ArticleContent { get; set; }
[NotMapped]
public string ArticleImage { get; set; }
public int ViewsCount { get; set; }
public int CommentCount { get; set; }
public DateTime ArticleCreatedDate { get; set; }
public bool ArticleStatus { get; set; }
public ICollection<Comment> Comments { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public int Id { get; set; }
public User User { get; set; }
}
Models içindeki yeni resim class'ı =
public class ImageModel
{
public string ArticleTitle { get; set; }
public string ArticleContent { get; set; }
public IFormFile ArticleImage { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public DateTime ArticleCreatedDate { get; set; }
}
Controller tarafı =
[HttpGet]
public IActionResult ImageUpload()
{
return View();
}
[HttpPost]
public IActionResult ImageUpload(ImageModel p)
{
Article a = new Article();
if (p.ArticleImage != null)
{
var extension = Path.GetExtension(p.ArticleImage.FileName);
var newimage = Guid.NewGuid() + extension;
var location = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/BlogImageFiles/", newimage);
var stream = new FileStream(location, FileMode.Create);
p.ArticleImage.CopyTo(stream);
a.ArticleImage = newimage;
}
a.ArticleStatus = true;
a.ArticleTitle = p.ArticleTitle;
a.ArticleCreatedDate = p.ArticleCreatedDate;
am.Add(a);
return RedirectToAction("Index");
}
}
View tarafı =
@model BlogUI.Areas.Admin.Models.ImageModel
@{
ViewData["Title"] = "Add";
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<br />
<h2>Yeni Blog Ekle</h2>
<br />
<form class="form-group" method="post" enctype="multipart/form-data" asp-controller="Blog" asp-action="Add">
<div class="card">
<div class="card-body">
<div class="forms-sample">
<div class="form-group">
<h4>Blog Başlık</h4>
@Html.TextBoxFor(x => x.ArticleTitle, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.ArticleTitle, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<h4>Blog İçerik</h4>
@Html.TextAreaFor(x => x.ArticleContent, 7, 3, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.ArticleContent, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<h4>Blog Kategori</h4>
@Html.DropDownListFor(x => x.CategoryId, (List<SelectListItem>)ViewBag.c, new { @class = "form-control" })
</div>
<div class="form-group">
<h4>Blog Resmi</h4>
<input type="file" name="ArticleImage" />
</div>
<button class="btn btn-info">Blog Ekle</button>
<input type="button" class="btn btn-danger" value="Geri Dön" onclick="location.href = '@Url.Action("Index")'" />
</div>
</div>
</div>
</form>
[/CODE]