Component is a model,and Storage
is one of its definitons.Is there a way to use a parameter instead of Storage
?
public IActionResult Filtered(string parameter)
{
return View(viewModel.Where(x => x.Component.Storage != "").ToList());
}
CodePudding user response:
I am assuming that parameter is of type string
. This is just a sample code. You can customize it to your needs.
var res = from m in viewModel // I don't know what is inside this viewModel
where !String.IsNullOrEmpty(parameter)
select m;
return View(res);
CodePudding user response:
to get the parameter from the url add it to the function
public IActionResult Filtered(string parameter)
{
return View(viewModel.Where(x => x.Component.Storage != parameter).ToList());
}
CodePudding user response:
You can use reflection to get value by parameter like this
var component = new Component { Storage = "A1" };
var valueOfName = component["Name"];
Console.WriteLine(valueOfName);
public partial class Component
{
public string Storage { get; set; }
public object this[string propertyName]
{
get
{
var type = GetType();
var property = type.GetProperty(propertyName);
if (property == null) throw new Exception("Class donesn't have this property");
var value = property.GetValue(this, null);
return value;
}
private set{};
}
}
If you can modify the class
you don't need the partial
key word on Component class