Guilherme Oderdenge
Guilherme Oderdenge

Reputation: 5001

Add property and value to C# List

The goal

Add property and value to List. I'm using C#/MVC 4/Razor.

The problem

I have the following:

return PartialView("_CategoriesList", db.bm_product_categories.ToList());

Ok, all good here. My list returns me multiple columns from database and one of them is Category_Name. I want to "slugify" each category name with C# and then pass to view. I was thinking in something like this:

foreach (var categorySlug in db.bm_product_categories)
{
    db.bm_product_categories
       .Add({
              "Category_Name_Slugged":
              Helpers.Slugify(db.bm_product_categories.Category_Name)
           });
}

But, obviously, this syntax is wrong — I do not know what to do.

Ideas?

Upvotes: 2

Views: 1245

Answers (3)

StuartLC
StuartLC

Reputation: 107277

You can project the altered model using LINQ, such as:

var sluggifiedProjection = 
  db.bm_product_categories
    .ToList() // Need to materialize here. Ensure any filters applied before this.
    .Select(cat => new 
      {
        CategoryNameSlugged = Helpers.Slugify(cat.Category_Name) 
        // .. + other fields of bm_product_categories needed by your view
      });

 return PartialView("_CategoriesList", sluggifiedProjection);

The creates a list of an anonymous class - you could obviously also create a new typed ViewModel class as well for CategoryNameSlugged and share this class with your View. See also Can I pass an anonymous type to my ASP.NET MVC view?

Update

As per the linked post, you can either leave the projection as anonymous, and then in the Razor View, use a dynamic or reflection to access the CategoryNameSlugged property from the list, or alternatively (and preferably), you can use a strongly typed ViewModel class:

internal class SluggedCategoriesViewModel
{
    public string CategoryNameSlugged {get; set; }
    // Add any other properties from bm_product_categories that your view needs, e.g.
    public int CategoryId {get; set; }
}

And the projection now uses the strongly typed ViewModel class:

  .Select(cat => new SluggedCategoriesViewModel
    {
      CategoryNameSlugged = Helpers.Slugify(cat.Category_Name),
      CategoryId = cat.Category_Id // etc
    });

Upvotes: 1

srsyogesh
srsyogesh

Reputation: 609

You can use something like this , if you can make a copy of the data and work

var sluggifiedList = (from category in db.bm_product_categories  
select category.CategoryName.Slugify()).ToList();  

else

(from categoryName in db.bm_product_categories.CategoryName      
select categoryName).ToList().ForEach  
(category_Name => category_Name = category_Name.Slugigy());

Upvotes: 2

Sam Leach
Sam Leach

Reputation: 12956

List<bm_product_categories> list = new List<bm_product_categories>();

foreach(string name in list.Select(x => x.Category_Name))
{ 
    slugifiedList.Add(name.Slugify()); // make extension method
}

or copy the List<bm_product_categories> and change the Category_Name property

Upvotes: 0

Related Questions