2

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?

svick
  • 236,525
  • 50
  • 385
  • 514
Guilherme Oderdenge
  • 4,935
  • 6
  • 61
  • 96

3 Answers3

2

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());
nawfal
  • 70,104
  • 56
  • 326
  • 368
srsyogesh
  • 609
  • 5
  • 17
1

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
    });
Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • `.Category_Name` from `db.bm_product_categories` was not found — what do I have to do? – Guilherme Oderdenge Jun 14 '13 at 14:29
  • Oh, Stuart, thanks! But my `Slugify()` method has a problem: `LINQ to Entities does not recognize the method 'System.String Slugify(System.String)' method, and this method cannot be translated into a store expression.`. What do you think about this? – Guilherme Oderdenge Jun 14 '13 at 14:34
  • My bad again ... you will need to materialize your db.bm_product_categories before projecting it. Updated. – StuartLC Jun 14 '13 at 14:35
  • No problem, Stuart. But, in my view, how will be the Model? – Guilherme Oderdenge Jun 14 '13 at 14:46
  • Oh, thanks! Thank you so much, really really! Just one last question: this internal classe — what is the best place to put it? – Guilherme Oderdenge Jun 14 '13 at 15:01
  • I would suggest you create a new folder in your MVC project called 'ViewModels' and put classes like this there (the namespace might change). – StuartLC Jun 14 '13 at 15:18
  • Okay! Oh, an unforeseen happened... I do not know how @model, at View, have to be — can you clarify this for me? Anyway, thank you very much again! – Guilherme Oderdenge Jun 14 '13 at 15:20
0
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

Sam Leach
  • 12,746
  • 9
  • 45
  • 73