coolwolf / 27/09/2018

MVC’de Controller açıklamaları eklemek ve bu açıklamaları listelemek

Controller adının üzerine Description attribute ekliyoruz:

[Description("Benim Çok Çalışan Kontrollerim")]
public class MyController : Controller
{
}

ardından uygulamamızdaki tüm controller’leri ve içlerindeki metodları listelemek için aşağıdaki fonksiyonu kullanabiliriz:
 

Assembly assembly = Assembly.GetExecutingAssembly();
IEnumerable types = assembly.GetTypes().Where(type => typeof(Controller).IsAssignableFrom(type)).OrderBy(x => x.Name);
List _controllers = new List();
List _methods = new List();
foreach (Type cls in types)
{
	AttributeCollection attributes =TypeDescriptor.GetProperties(this)[cls.Name].Attributes;
	DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
	_controllers.Add(new SelectListItem() { Text= myAttribute.Description, Value= cls.Name.Replace("Controller", "") });
	IEnumerable memberInfo = cls.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public).Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any()).OrderBy(x => x.Name);
	foreach (MemberInfo method in memberInfo)
	{
		if (method.ReflectedType.IsPublic && !method.IsDefined(typeof(NonActionAttribute)))
		{
			_methods.Add(new SelectListItem() { Text=method.Name.ToString() });
		}
	}
}

coolwolf / 22/03/2018

MVC @Html.DropDownList SelectedValue does not work

I am new to mvc. When i create @Html.DropDownList, i see sometimes i can set SelectedValue but sometimes i can not.
I tried many solutions like creating “<select” manually, passing generic List<>, passing List<SelectListItem>, passing SelectList etc.
But no success. Razor View does no select the item which i want.
After days of searching, i found a blog entry which solve my problem.
My controller side was like that:

public ActionResult Liste(int? MyValue)
{
	if (MyValue== null || MyValue == 0) MyValue = 1;
	List<SelectListItem> _sl = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(v => new SelectListItem{Text = v.ToString(),Value = ((int)v).ToString()}).ToList();
	foreach(SelectListItem _itm in _sl)
	{
		if (_itm.Value == MyValue.Value.ToString()) _itm.Selected = true;
	}
	ViewBag.ProductType= _sl;
	ViewBag.SelValue= MyValue;
	return View();
}

and my Razor View was like that:

@Html.DropDownList("ProductType", (List<SelectListItem>)ViewBag.ProductType, new { @class = "form-control" })

normally there is no problem. Problem occurs only when i try to set selected item. This completely works.
So what is the problem then?
The problem is: The name of dropdown and viewbag constant is same. 
When i change my controller to this:

public ActionResult Liste(int? MyValue)
{
	if (MyValue== null || MyValue == 0) MyValue = 1;
	List<SelectListItem> _sl = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(v => new SelectListItem{Text = v.ToString(),Value = ((int)v).ToString()}).ToList();
	foreach(SelectListItem _itm in _sl)
	{
		if (_itm.Value == MyValue.Value.ToString()) _itm.Selected = true;
	}
	ViewBag.MyProdList= _sl;
	ViewBag.SelValue= MyValue;
	return View();
}

and View to this:

@Html.DropDownList("ProductType", (List<SelectListItem>)ViewBag.MyProdList, new { @class = "form-control" })

everything works.
I wonder, why did i not understand this before.
If anyone face same problem, i hope he/she find this blog entry and not spent lot of time as i spent.