PDA

View Full Version : خطای مربوط به Partialview



mehliaeb
چهارشنبه 24 بهمن 1397, 08:58 صبح
سلام دوستان من دو کلاس دارم
Product



public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity )]
public int ProductId { get; set; }

[MaxLength(100)]
[Display(Name = "نام کالا")]
[Required(ErrorMessage = "نام باید وارد شود")]
[StringLength(10, MinimumLength = 5, ErrorMessage = "تعداد کاراکترها باید بین 5 تا 10 عدد باشد")]
public string ProductName { get; set; }

[Display(Name = "قیمت کالا")]
[Required(ErrorMessage = "قیمت کالا باید وارد شود")]

public decimal ProductPrice { get; set; }

[Display(Name = "توضیحات")]
public string ProductDescription { get; set; }

[Display(Name = "تصویر")]
public byte[] ImageUpload { get; set; }
}


login



[Display(Name = "نام کاربری")]
[Required(ErrorMessage = "لطفا {0} را وارد کنید")]
public string UserName { get; set; }

[Display(Name = "پسورد")]
[Required(ErrorMessage = "لطفا {0} را وارد کنید")]
[DataType(DataType.Password)]
public string Password { get; set; }

[Display(Name = " به یاد داشته باش")]
public bool RememberMe { get; set; }


contoller



public ActionResult ProductList()
{
try
{
var ProductList = (from s in db.Products select s).ToList();
return View(ProductList);
}
catch (Exception)
{

throw;
}
}


viewProductList



@model IEnumerable<test11.Models.Product>

@{
ViewBag.Title = "ProductList";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

</head>
<body style="margin-left:300px">
<br />
@*<input id="myInput" type="text" placeholder="جستجو..." class="search">*@

<div class="container">
<div class="row">
<div class="column" style="background-color:lightskyblue;"> @Html.DisplayNameFor(model => model.ProductName) </div>
<div class="column " style="background-color:lightskyblue;">@Html.DisplayNameFor(model => model.ProductPrice)</div>
<div class="column" style="background-color:lightskyblue;"> @Html.DisplayNameFor(model => model.ProductDescription) </div>
<div class="column " style="background-color:lightskyblue;"> @Html.DisplayNameFor(model => model.ImageUpload) </div>
<div class="column " style="background-color:lightskyblue;">ویرایش</div>
<div class="column " style="background-color:lightskyblue;">حذف</div>
</div>
<br />
@foreach (var item in Model)
{
<div class="row " id="mydiv" style="background-color:aliceblue;">
<panel>
<div class="column">@item.ProductName</div>
<div class="column">@item.ProductPrice</div>
<div class="column">@item.ProductDescription</div>
<div class="column">
<img class="img" src="@Url.Action("RetrieveImage", "Product", new { id = item.ProductId})" />
</div>
@*ویرایش*@
<div class="column">

<!-- Trigger the modal with a button -->
<a>
<span class="glyphicon glyphicon-edit" data-toggle="modal" data-target="#EditModal@(item.ProductId)"></span>
</a>
<!-- Modal -->
<div class="modal fade" id="EditModal@(item.ProductId)" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title panel-primary" dir="rtl">ویرایش کالا</h4>
</div>
<div class="modal-body form-group-sm">

<div class="form-group hidden" dir="rtl">
@Html.LabelFor(model => item.ProductId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10 hidden">
@Html.EditorFor(model => item.ProductId, new { htmlAttributes = new { @class = "form-control", id = "ProductId" + item.ProductId } })
@Html.ValidationMessageFor(model => item.ProductId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" dir="rtl">
<div class="col-md-10" dir="rtl">
@Html.EditorFor(model => item.ProductName, new { htmlAttributes = new { @class = "form-control", id = "ProductName" + item.ProductId } })
@Html.ValidationMessageFor(model => item.ProductName, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => item.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })

</div>
<hr />
<div class="form-group" dir="rtl">
<div class="col-md-10">
@Html.EditorFor(model => item.ProductPrice, new { htmlAttributes = new { @class = "form-control", id = "ProductPrice" + item.ProductId } })
@Html.ValidationMessageFor(model => item.ProductPrice, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => item.ProductPrice, htmlAttributes: new { @class = "control-label col-md-2" })
</div>
<hr />
<hr />

<div class="form-group" dir="rtl">
<div class="col-md-10">
@Html.EditorFor(model => item.ProductDescription, new { htmlAttributes = new { @class = "form-control", id = "ProductDescription" + item.ProductId } })
@Html.ValidationMessageFor(model => item.ProductDescription, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => item.ProductDescription, htmlAttributes: new { @class = "control-label col-md-2" })
</div>
<hr />
<hr />
<div class="form-group" dir="rtl">
<div class="col-md-10">
<img class="img" id="myImg" src="@Url.Action("RetrieveImage", "Home", new { id = item.ProductId})" />
</div>
</div>
<div class="form-group" dir="rtl" id="fileDiv">
<input type="file" id="imageInput@(item.ProductId)" href="@Url.Action("","",new { id=item.ProductId})" onchange="EditImage(@item.ProductId)" />

</div>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-sm" data-dismiss="modal">انصراف</button>
<button type="button" class="btn btn-primary btn-sm btnEdit" id="btnEdit" onclick=" edit(@(item.ProductId)) " data-dismiss="modal">ذخیره تغییرات</button>
</div>
</div>

</div>
</div>
</div>
@*حذف*@
<div class="column">
<!-- Trigger the modal with a button -->
<span class="glyphicon glyphicon-remove-circle" data-toggle="modal" data-target="#DeleteModal@(item.ProductId)"></span>
<!-- Modal -->
<div class="modal fade" id="DeleteModal@(item.ProductId)" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title" dir="rtl">حذف کالا</h4>
</div>
<div class="modal-body">
<div class="form-group hidden" dir="rtl">
@Html.LabelFor(model => item.ProductId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10 hidden">
@Html.EditorFor(model => item.ProductId, new { htmlAttributes = new { @class = "form-control ", id = "ProductId" + item.ProductId } })
@Html.ValidationMessageFor(model => item.ProductId, "", new { @class = "text-danger" })
</div>
</div>
<p>آیا از حذف کالا مطمئن هستید؟</p>
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-sm" data-dismiss="modal">خیر</button>
<button type="button" class="btn btn-primary btn-sm" data-dismiss="modal" id="btnDelete" onclick="deleteProduct(@(item.ProductId))">بله</button>
@if (null != TempData["deleteMessage"])
{
<script type="text/javascript">
alert("@TempData["deleteMessage"]");
</script>
}
</div>
</div>
</div>
</div>
</div>
</div>

</panel>
<div class="w-100 d-none d-md-block"></div>
</div>
<br />
}
</div>
<div class="row">
<div class="col-sm-12">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#CreateModal">افزودن</button>

<!-- Modal -->
<div class="modal fade" id="CreateModal" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title" dir="rtl">افزودن کالا</h4>
</div>
<div class="modal-body" id="myCreatediv" enctype="multipart-">
<div class="form-group" dir="rtl">

<label for="usr">@Html.DisplayNameFor(model => model.ProductName)</label>
<input type="text" class="form-control" id="ProductName" name="ProductName">
</div>
<div class="form-group" dir="rtl">
<label>@Html.DisplayNameFor(model => model.ProductPrice)</label>
<input type="text" class="form-control" id="ProductPrice" name="ProductPrice">
</div>
<div class="form-group" dir="rtl">
<label> @Html.DisplayNameFor(model => model.ProductDescription)</label>
<input type="text" class="form-control" id="ProductDescription" name="ProductDescription">
</div>

<div class=" form-group" dir="rtl">
<label> @Html.DisplayNameFor(model => model.ImageUpload)</label>
<input type="file" id="fileInput" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-sm" data-dismiss="modal" onclick="Create()">ذخیره تغییرات</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br />
</body>
</html>


view Layout


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>

<div class="container body-content">
@Html.Partial("_nav")
@RenderBody()
<hr />
<footer>
<p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>




view _nav(Partialview)



@model test11.Models.Login
<body>


@*NavBar*@
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-header">
<a class="navbar-brand" href="#">خرید آنلاین</a>
<button onclick="document.getElementById('Cart').style.display='blo ck'" class="button" style="width:auto;">
<span class="glyphicon glyphicon-shopping-cart"></span> سبد خرید
</button>
<div id="Cart" class="modal">

<form class=" modal-content-Register animate" action="/action_page.php">
<div class="modal-header">
<span onclick="document.getElementById('Cart').style.display='non e'" class="close" title="Close Modal">&times;</span>
<h4 class="modal-title" dir="rtl">لیست خرید</h4>
</div>
<div class="modal-body">


</div>
</form>
</div>
</div>


@if (!Request.IsAuthenticated)
{
<button class="button " style="width:auto;" onclick="GoHome()">صفحه اصلی</button>

<button class="button" style="width:auto;" onclick="GoRegister()">ثبت نام</button>
@* ModalLogin *@
<button onclick="document.getElementById('Login').style.display='bl ock'" class="button" style="width:auto;">ورود</button>
<div id="Login" class="modal modal1">
@using (Html.BeginForm("Login", "Account", "Role", FormMethod.Post))
{
<form id="Login" class="modal-content-Login animate">
<div class="modal-header">
<span onclick="document.getElementById('Login').style.display='no ne'" class="close" title="Close Modal">&times;</span>
<h4 class="modal-title" dir="rtl">فرم ورود</h4>
</div>
<div class="modal-body">

@Html.AntiForgeryToken()

<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
</div>

<div class="form-group">
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-primary">ورود</button>
</div>
</div>
</div>
</div>
</form>
}

</div>
<input id="myInput" type="text" placeholder="جستجو..." class="search" />


}
else
{
<button class="button " style="width:auto;" onclick="GoHome()">صفحه اصلی</button>
<input id="myInput" type="text" placeholder="جستجو..." class="search" />

<div class="row">
<div class="column1" style="color:white">خوش آمدید @User.Identity.Name</div>
<div class="column1">
<a href="@Url.Action("SignOut","Account","Role")" class="">خروج</a>
</div>
</div>
}

}

</nav>
</body>






پیغام خطا


The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ProjectOnlineShop.Areas.Product.Models.Product]', but this dictionary requires a model item of type 'ProjectOnlineShop.Areas.Role.Models.LoginViewMode l'.

لطفا راهنمایی کنید

rahmatipoor
چهارشنبه 24 بهمن 1397, 10:39 صبح
سلام
شما توی ویوتون از layout استفاده کردید که توی layout پارشیال ویو view _nav قرار داره که یک مدل از نوع LoginViewMode رو قبول میکنه . اما مدل مربوط به viewProductList از نوع Product هست.
بنابراین موقع اجرای ویو این خطا رو داده و گفته که پارشیال ویوی شما نیاز به مدلی از نوع LoginViewMode داره اما مدلی که پاس داده شده از نوع Product هست.

mehliaeb
چهارشنبه 24 بهمن 1397, 11:03 صبح
سلام
شما توی ویوتون از layout استفاده کردید که توی layout پارشیال ویو view _nav قرار داره که یک مدل از نوع LoginViewMode رو قبول میکنه . اما مدل مربوط به viewProductList از نوع Product هست.
بنابراین موقع اجرای ویو این خطا رو داده و گفته که پارشیال ویوی شما نیاز به مدلی از نوع LoginViewMode داره اما مدلی که پاس داده شده از نوع Product هست.


سلام
ممنون از پاسختون
راه حلتون چیه؟

mehliaeb
چهارشنبه 24 بهمن 1397, 11:07 صبح
من باید لاگین رو تو پارشیال ویو داشته باشم تا بتونم اطلاعات لاگین رو بگیرم !!