sunn789
سه شنبه 30 مهر 1392, 23:51 عصر
سلام دوستان
به یک مشکل برخوردم دو روزه تو تاپیکهای ماکروسفت هم رفتم چیزی ندیدم
این model
public class PublicObject
{
[Key]
public int ObjectID { get; set; }
[Required]
[MaxLength(50)]
public string ObjectName { get; set; }
[Required]
[MaxLength(4000)]
public string Description { get; set; }
public virtual PublicContent publicContents { get; set; }
}
public class PublicContent
{
public int id { get; set; }
[Required]
[Display(Name="موضوع")]
public string E_Subject { get; set; }
[Required]
[Display(Name = "خلاصه")]
public string E_ShortText { get; set; }
[Required]
public string E_Title { get; set; }
[Required]
public string E_Text { get; set; }
[Required]
public string E_Image { get; set; }
public DateTime E_CreateDate { get; set; }
public virtual IList<User> Users { get; set; }
[Required]
public bool Show { get; set; }
[Required]
public bool ShowInFirstPage { get; set; }
public int Objects { get; set; }
public IEnumerable<SelectListItem> PublicObjects { get; set; }
}
}
کنترل
public ActionResult Edit(int id = 0)
{
PublicContent publiccontent = db.PublicContents.Find(id);
//IEnumerable<SelectListItem> ob =
ViewBag.ObjectList = new SelectList(db.PublicObjects, "ObjectID", "ObjectName").ToList();
if (publiccontent == null)
{
return HttpNotFound();
}
return View(publiccontent);
}
و این هم ویو
<div class="editor-label">
@Html.LabelFor(model => model.Objects)
</div>
<div class="editor-field">
@Html.DropDownList("Objects",string.Empty)
@Html.ValidationMessageFor(model => model.Objects)
</div>
البته این رو هم و خیلی دیگر رو هم امتحان کردم
<div class="editor-label">
@Html.LabelFor(model => model.Objects)
</div>
<div class="editor-field">
@Html.DropDownListFor(model=>model.PublicObjects,ViewBag.ObjectList as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.Objects)
</div>
اما زمانی که برنامه اجرا میشود و روی ذخیره تغییرات کلیک میکنم مثل این دارم
The ViewData item that has the key 'Objects' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
و اگه تو کدها باز هم تغییر بدم یکی از حالتها اینکه لیست باز شو پس از زدن دکمه ذخیره تغییرات خالی میشه
لطفاً دو روزه از کار و زندگی افتادم و یا اگه مشکل از کل کاره یه روش استاندارد بفرمایید تا لیست باز شو رو پر کنم و ID اون متن رو توی جدول ذخیره کنم
با تشکر
به یک مشکل برخوردم دو روزه تو تاپیکهای ماکروسفت هم رفتم چیزی ندیدم
این model
public class PublicObject
{
[Key]
public int ObjectID { get; set; }
[Required]
[MaxLength(50)]
public string ObjectName { get; set; }
[Required]
[MaxLength(4000)]
public string Description { get; set; }
public virtual PublicContent publicContents { get; set; }
}
public class PublicContent
{
public int id { get; set; }
[Required]
[Display(Name="موضوع")]
public string E_Subject { get; set; }
[Required]
[Display(Name = "خلاصه")]
public string E_ShortText { get; set; }
[Required]
public string E_Title { get; set; }
[Required]
public string E_Text { get; set; }
[Required]
public string E_Image { get; set; }
public DateTime E_CreateDate { get; set; }
public virtual IList<User> Users { get; set; }
[Required]
public bool Show { get; set; }
[Required]
public bool ShowInFirstPage { get; set; }
public int Objects { get; set; }
public IEnumerable<SelectListItem> PublicObjects { get; set; }
}
}
کنترل
public ActionResult Edit(int id = 0)
{
PublicContent publiccontent = db.PublicContents.Find(id);
//IEnumerable<SelectListItem> ob =
ViewBag.ObjectList = new SelectList(db.PublicObjects, "ObjectID", "ObjectName").ToList();
if (publiccontent == null)
{
return HttpNotFound();
}
return View(publiccontent);
}
و این هم ویو
<div class="editor-label">
@Html.LabelFor(model => model.Objects)
</div>
<div class="editor-field">
@Html.DropDownList("Objects",string.Empty)
@Html.ValidationMessageFor(model => model.Objects)
</div>
البته این رو هم و خیلی دیگر رو هم امتحان کردم
<div class="editor-label">
@Html.LabelFor(model => model.Objects)
</div>
<div class="editor-field">
@Html.DropDownListFor(model=>model.PublicObjects,ViewBag.ObjectList as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.Objects)
</div>
اما زمانی که برنامه اجرا میشود و روی ذخیره تغییرات کلیک میکنم مثل این دارم
The ViewData item that has the key 'Objects' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
و اگه تو کدها باز هم تغییر بدم یکی از حالتها اینکه لیست باز شو پس از زدن دکمه ذخیره تغییرات خالی میشه
لطفاً دو روزه از کار و زندگی افتادم و یا اگه مشکل از کل کاره یه روش استاندارد بفرمایید تا لیست باز شو رو پر کنم و ID اون متن رو توی جدول ذخیره کنم
با تشکر