speralda
سه شنبه 10 دی 1392, 10:25 صبح
درود دوستان
من نیاز دارم که یک dropdownlistدر view بزارم
این کارو هم انجام دادم ولی نمیدونم عملیات create و edit رو چجوری کد بزنم
در controller
public ActionResult Create()
{
List<ProductGroups> lstGroups;
lstGroups = db.ProductGroups.ToList();
ViewBag.GroupsList = lstGroups;
return View();
}
در view
@model Eshop_SportS.Models.Products
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
....
...
<tr class="tr">
<td align="right" class="style2">
@Html.LabelFor(model => model.ProductGroups.ProductGroupTitle):
</td>
<td class="style3" rowspan="1">
@Html.DropDownListFor(model => model.ProductGroups.ProductGroupId, new SelectList(ViewBag.GroupsList, "ProductGroupId", "ProductGroupTitle"), "گروهی را انتخاب کنید")
</td>
</tr>
...
...
در model کلاس product
namespace Eshop_SportS.Models
{
public class Products
{
[Key]
[HiddenInput(DisplayValue = false)]
public int ProductId { set; get; }
[Display(Name = "نام محصول")]
[Required(ErrorMessage = "لطفا نام محصول را وارد نمایید")]
[StringLength(32)]
public string ProductTitle { set; get; }
[Display(Name = "توضیحات")]
[Required(ErrorMessage = "لطفا توضیحات را وارد نمایید")]
[StringLength(4000)]
public string ProductDescription { set; get; }
[Display(Name = "قیمت محصول")]
[Required(ErrorMessage = "لطفا قیمت محصول را وارد نمایید")]
public int ProductPrice { set; get; }
[Display(Name = "مسیر تصویر ")]
[StringLength(50)]
public string ProductImageUrl { set; get; }
public virtual ProductGroups ProductGroups { set; get; }
public List<OrderDetails> OrderDetails { set; get; }
}
}
در model کلاس ProductGroups
namespace Eshop_SportS.Models
{
public class ProductGroups
{
[Key]
[HiddenInput(DisplayValue = false)]
public int ProductGroupId { set; get; }
[Display(Name = "گروه محصول")]
[Required(ErrorMessage = "لطفا نامی برای گروه محصول وارد کنید")]
[StringLength(32)]
public string ProductGroupTitle { set; get; }
public List<Products> Products { set; get; }
}
}
ولی با این error برخورد میکنم
Value cannot be null.
Parameter name: items
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items
Source Error:
Line 47: </td> Line 48: <td class="style3" rowspan="1"> Line 49: @Html.DropDownListFor(model => model.ProductGroups.ProductGroupTitle, new SelectList(ViewBag.GroupsList, "ProductGroupId", "ProductGroupTitle"), "گروهی را انتخاب کنید") Line 50: </td> Line 51: </tr>
من نیاز دارم که یک dropdownlistدر view بزارم
این کارو هم انجام دادم ولی نمیدونم عملیات create و edit رو چجوری کد بزنم
در controller
public ActionResult Create()
{
List<ProductGroups> lstGroups;
lstGroups = db.ProductGroups.ToList();
ViewBag.GroupsList = lstGroups;
return View();
}
در view
@model Eshop_SportS.Models.Products
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
....
...
<tr class="tr">
<td align="right" class="style2">
@Html.LabelFor(model => model.ProductGroups.ProductGroupTitle):
</td>
<td class="style3" rowspan="1">
@Html.DropDownListFor(model => model.ProductGroups.ProductGroupId, new SelectList(ViewBag.GroupsList, "ProductGroupId", "ProductGroupTitle"), "گروهی را انتخاب کنید")
</td>
</tr>
...
...
در model کلاس product
namespace Eshop_SportS.Models
{
public class Products
{
[Key]
[HiddenInput(DisplayValue = false)]
public int ProductId { set; get; }
[Display(Name = "نام محصول")]
[Required(ErrorMessage = "لطفا نام محصول را وارد نمایید")]
[StringLength(32)]
public string ProductTitle { set; get; }
[Display(Name = "توضیحات")]
[Required(ErrorMessage = "لطفا توضیحات را وارد نمایید")]
[StringLength(4000)]
public string ProductDescription { set; get; }
[Display(Name = "قیمت محصول")]
[Required(ErrorMessage = "لطفا قیمت محصول را وارد نمایید")]
public int ProductPrice { set; get; }
[Display(Name = "مسیر تصویر ")]
[StringLength(50)]
public string ProductImageUrl { set; get; }
public virtual ProductGroups ProductGroups { set; get; }
public List<OrderDetails> OrderDetails { set; get; }
}
}
در model کلاس ProductGroups
namespace Eshop_SportS.Models
{
public class ProductGroups
{
[Key]
[HiddenInput(DisplayValue = false)]
public int ProductGroupId { set; get; }
[Display(Name = "گروه محصول")]
[Required(ErrorMessage = "لطفا نامی برای گروه محصول وارد کنید")]
[StringLength(32)]
public string ProductGroupTitle { set; get; }
public List<Products> Products { set; get; }
}
}
ولی با این error برخورد میکنم
Value cannot be null.
Parameter name: items
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items
Source Error:
Line 47: </td> Line 48: <td class="style3" rowspan="1"> Line 49: @Html.DropDownListFor(model => model.ProductGroups.ProductGroupTitle, new SelectList(ViewBag.GroupsList, "ProductGroupId", "ProductGroupTitle"), "گروهی را انتخاب کنید") Line 50: </td> Line 51: </tr>