Remove HTML tags in IQueryable Linq query result in asp.net mvc

I'm having following Linq query

    public ActionResult Dashboard()
    {

               var query = from product in db.AB_Product
                    join productvsfield in db.AB_Product_vs_Field on product.ProductID equals productvsfield.Product_ID
                    join type in db.AB_ProductType on product.ProductTypeID equals type.ProductTypeID
                    where productvsfield.Field_ID == "F000001"
                    select new DashboardViewModel

                    {
                         Product_ID = product.ProductID,
                         Product_Title_EN = productvsfield.Field_Value_EN,
                         Product_Title_AR = productvsfield.Field_Value_AR,
                         Product_Type_EN = type.ProductTypeNameEn,                             
                         Product_Type_AR = type.ProductTypeNameAr,
                         ModifiedDate = product.UpdatedDate,
                    };

        return View(query);

    }

for that query I'm getting view like below

since Its contains some HTML tags, I used to change my code to like below

    public ActionResult Dashboard()
    {

               var query = from product in db.AB_Product
                    join productvsfield in db.AB_Product_vs_Field on product.ProductID equals productvsfield.Product_ID
                    join type in db.AB_ProductType on product.ProductTypeID equals type.ProductTypeID
                    where productvsfield.Field_ID == "F000001"
                    select new DashboardViewModel

                    {
                         Product_ID = product.ProductID,
                         Product_Title_EN = productvsfield.Field_Value_EN,
                         Product_Title_AR = productvsfield.Field_Value_AR,
                         Product_Type_EN = type.ProductTypeNameEn,                             
                         Product_Type_AR = type.ProductTypeNameAr,
                         ModifiedDate = product.UpdatedDate,
                    };

        var filtered = Regex.Replace(query, @"<[^>]+>|&nbsp;", "").Trim();

        return View(filtered);

    }

using regex I try to do this.but then I'm getting unexpected view like this

Answers


Problem is that you applying Replace on query object, but you need do replace on fields where you have HTML e.g.:

var result = (from product in db.AB_Product
    join productvsfield in db.AB_Product_vs_Field on product.ProductID equals productvsfield.Product_ID
    join type in db.AB_ProductType on product.ProductTypeID equals type.ProductTypeID
    where productvsfield.Field_ID == "F000001"
    select new DashboardViewModel

    {
         Product_ID = product.ProductID,
         Product_Title_EN = productvsfield.Field_Value_EN,
         Product_Title_AR = productvsfield.Field_Value_AR,
         Product_Type_EN = type.ProductTypeNameEn,                             
         Product_Type_AR = type.ProductTypeNameAr,
         ModifiedDate = product.UpdatedDate,
    }).ToList();


result = result.Select(x => new DashboardViewModel
    {
         Product_ID = x.ProductID,
         Product_Title_EN = Regex.Replace(x.Product_Title_EN, @"<[^>]+>|&nbsp;", "").Trim(),
         Product_Title_AR = Regex.Replace(x.Product_Title_AR, @"<[^>]+>|&nbsp;", "").Trim(),
         Product_Type_EN = x.Product_Type_EN,                             
         Product_Type_AR = x.Product_Type_AR,
         ModifiedDate = x.ModifiedDate,
    }).ToList()

return View(result);

Need Your Help

Unit testing for void methods and threads in jUnit

java unit-testing testing junit

I'm new to unit testing. I understood the principles of it, but I still can't figure out how to test my current project. I need to test void methods, operating with java.nio.SocketChannel. These me...