SelectList'e dışarıdan option eklemek?
Merhabalar kategorilemede parentId mantığını kullanmaya çalıştım. Kategorimi düzenlemek istediğimde eğer düzenle dediğim kategorinin parentId si 0 ise option da seçili olarak gelmesini istiyorum. Fakat AnaMenü adında bir option um yok. Bunu dışarıdan nasıl ekleyebilirim? Listelediğim SelectList i me dışarıdan nasıl ekleme yapabilirim?
https://sorucevap.acikakademi.com/storage/temp/1289-seciniz.png
CategoryController
///////// EDIT START ///////////////////////////////////////////////////
private void SetCategoryListEdit(object category = null)
{
var categoryList = _categoryRepository.GetAll().ToList();
var selectList = new SelectList(categoryList, "CategoryId", "CategoryName", category);
ViewData.Add("ParentId", selectList);
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var category = _categoryRepository.GetById(id.Value);
if (category == null)
{
return HttpNotFound();
}
SetCategoryListEdit(category.ParentId);
return View(category);
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit(Category category)
{
if (!ModelState.IsValid)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SetCategoryListEdit(category.ParentId);
_categoryRepository.Update(category);
_categoryRepository.Save();
return RedirectToAction("Index");
}
///////// EDIT FINISH ///////////////////////////////////////////////////
View
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.CategoryId)
@Html.LabelFor(model => model.CategoryName)
@Html.EditorFor(model => model.CategoryName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CategoryName, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.ParentId)
@Html.DropDownList("ParentId", null, null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ParentId, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.CategoryOrder)
@Html.EditorFor(model => model.CategoryOrder, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CategoryOrder, "", new { @class = "text-danger" })
https://sorucevap.acikakademi.com/storage/temp/1289-seciniz.png
CategoryController
///////// EDIT START ///////////////////////////////////////////////////
private void SetCategoryListEdit(object category = null)
{
var categoryList = _categoryRepository.GetAll().ToList();
var selectList = new SelectList(categoryList, "CategoryId", "CategoryName", category);
ViewData.Add("ParentId", selectList);
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var category = _categoryRepository.GetById(id.Value);
if (category == null)
{
return HttpNotFound();
}
SetCategoryListEdit(category.ParentId);
return View(category);
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit(Category category)
{
if (!ModelState.IsValid)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SetCategoryListEdit(category.ParentId);
_categoryRepository.Update(category);
_categoryRepository.Save();
return RedirectToAction("Index");
}
///////// EDIT FINISH ///////////////////////////////////////////////////
View
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.CategoryId)
@Html.LabelFor(model => model.CategoryName)
@Html.EditorFor(model => model.CategoryName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CategoryName, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.ParentId)
@Html.DropDownList("ParentId", null, null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ParentId, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.CategoryOrder)
@Html.EditorFor(model => model.CategoryOrder, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CategoryOrder, "", new { @class = "text-danger" })