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?

3 Answers 3

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());
Sign up to request clarification or add additional context in comments.

5 Comments

Whoever downvoted this is pretty smart to decipher this in seconds.
Hello. In my view my model was @model IEnumerable<BluMercados.Models.Data.bm_product_categories> — And now, as it will be?
i think you dont have to too worry about where to place this internal class , u can create a new cs file and place this code or add this new class in any of the existing file , of course in the same project
@srsyogesh My question is: what I have to put on the first line of my View?
I havent learned asp.net yet :( so i am not sure what to specify in the View.
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
    });

7 Comments

.Category_Name from db.bm_product_categories was not found — what do I have to do?
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?
My bad again ... you will need to materialize your db.bm_product_categories before projecting it. Updated.
No problem, Stuart. But, in my view, how will be the Model?
Oh, thanks! Thank you so much, really really! Just one last question: this internal classe — what is the best place to put it?
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.