Dan
Dan

Reputation: 393

C# Contains Method

My site has a column that displays the news. The DIV-s with these news which contain the word "prize" must be painted in the green color. (If a person has created a record (a news) without specifying the Prize (without word "Prize"), the green is not necessary. But if a person filled out the field model.Prize (so in the text we have a word "Prize") the div must be painted in green color.

In a view for creating news there is a field model.Prize

<div class="editor-field">
     @Html.TextAreaFor(model => model.Prize,4,55,null)
     @Html.ValidationMessageFor(model => model.Prize)
</div>

The value of model.Prize takes the Controller which create a new news record.

public ActionResult Create(Project project)
{
    if (ModelState.IsValid)
        {(some code...)
        News n = new News();
        n.Date = DateTime.UtcNow;
        n.Description = project.Shortdescription+"\r\n\Prize:\r\n"+project.Prize;

       (some code…)
       NewsController.Add(db,n);
       db.SaveChanges();
       return RedirectToAction("Main");
   }

In the another method Block of News Controller I display the news:

public PartialViewResult Block()
{
    List<News> news = new List<News>();
    Int32 count = 0;

    foreach (News n in db.News.ToList().OrderByDescending(n => n.Date))
    {
        if (count++ < 13) news.Add(n);
    } 
    return PartialView(news);

For each entry in the View Block creates <div class ="newsWrapper"> in which the news record insert.

@foreach (var item in Model){
    <div class ="newsWrapper">
    <p class="newsDate">@item.Date.AddHours(4).ToString("dd.MM.yyyy HH:mm")</p>
    <a href="@item.Link"> @item.Title </a>
    <p>@Html.Raw(Html.Encode(item.Description).Replace("\n", "<br />"))</p>
</div> 
}

I tried to solve the problem I added the new div in the Block View:

@foreach (var item in Model)
{   
<div class ="newsWrapper">
        <div class="@(ViewBag.IsPrize == true ? "GreenNewsClass" : "")">
            <p class="newsDate">@item.Date.AddHours(4).ToString("dd.MM.yyyy HH:mm")</p>
                <a href="@item.Link"> @item.Title </a>
            <p>@Html.Raw(Html.Encode(item.Description).Replace("\n", "<br />"))</p>
        </div>
</div>
}

The GreenNewsClass will paint this div in green color.

But how can I get ViewBag.IsPrize == true if n.Description contains the word Prize, and ViewBag.IsPrize == false if it's not?

I tried to change the method Block:

public PartialViewResult Block()
{
    List<News> news = new List<News>();
    Int32 count = 0;

    foreach (News n in db.News.ToList().OrderByDescending(n => n.Date))
    {
        if (count++ < 13) news.Add(n);
        if (n.Description.Contains("Призы"))
        {
            ViewBag.IsPrize = true;
        }
        else { ViewBag.IsPrize = false; }
    }  
    return PartialView(news);

but it paints all news in green color, not only those which contain the word Prize.

Upvotes: 5

Views: 281

Answers (2)

pedrommuller
pedrommuller

Reputation: 16066

First try to add a property to your model instead to your ViewBag, it seems like you only have a single value in your ViewBag.

Remove the true condition because it's redundant, move the class definition inside the condition that way the div will be empty when the condition is false and try the following:

@foreach (var item in Model)
{   
    <div class ="newsWrapper">
        <div @(item.IsPrize? "class=GreenNewsClass" : "")>
            <p class="newsDate">@item.Date.AddHours(4).ToString("dd.MM.yyyy HH:mm")</p>
            <a href="@item.Link"> @item.Title </a>
            <p>@Html.Raw(Html.Encode(item.Description).Replace("\n", "<br />"))</p>
        </div>
    </div>
}

I have not verified the code but try it out.

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149040

It sounds like you want to do this:

@foreach (var item in Model)
{   
  <div class ="newsWrapper">
    <div class="@(item.Description.Contains("Призы") ? "GreenNewsClass" : "")">
      <p class="newsDate">@item.Date.AddHours(4).ToString("dd.MM.yyyy HH:mm")</p>
      <a href="@item.Link"> @item.Title </a>
      <p>@Html.Raw(Html.Encode(item.Description).Replace("\n", "<br />"))</p>
    </div>
  </div>
}

Upvotes: 2

Related Questions