Solved

Linq query to conditionally show create field value if another field has a value

Posted on 2014-11-29
3
101 Views
Last Modified: 2016-02-16
Hi:

I'm trying to create a table showing meetings and beside each meeting, a button to click if there is a recording to download.

I first tried doing this in the cshtml page but had problems.  The code looks something like this:

@foreach (var item in Model) {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Start)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.End)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Recording.RecordingUrl)
        </td>
        <td>
            @if (!String.IsNullOrEmpty(modelItem => item.Recording.RecordingUrl))
            {
                <a class="btn btn-default btn-sm " href="/Meetings/DownloadRecording?item.Recording.RecordingUrl"><span class="glyphicon glyphicon-film" aria-hidden="true"></span></a>
            }
        </td>

    </tr>
}

Open in new window


It occurs to me that a better way to do this is to create a view model and populate my fields before they come down to the browser - but can't figure out how to do this.

My Meeting model looks like this:

public class Meeting
    {
        public int Id { get; set; }
        [MaxLength(40)]
        public string Title { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public Recording Recording { get; set; }
    }

Open in new window


I've created a viewmodel that looks like this:

public class MeetingsViewModel
    {
        public int Id { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public string RecordingUrl { get; set; }
        public string RecordingButtonField { get; set; }
    }

Open in new window


However, I'm stumped at this point - as you can see from the controller:

 public ActionResult Index()
        {
            var viewModel = new MeetingsViewModel
            {
                
            };

            return View(_db.Meetings
                .Include("Recording")
                .ToList());
        }

Open in new window


Any help in figuring this out how to populate the RecordingButtonField if there is a value in the RecordingUrl would be greatly appreciated.

RBS
0
Comment
Question by:RBS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 40472497
Just go back to your original approach. The problem you were having was with your if statement.

@if (!String.IsNullOrEmpty(item.Recording.RecordingUrl))
{
    <a class="btn btn-default btn-sm " href="/Meetings/DownloadRecording?@item.Recording.RecordingUrl"><span class="glyphicon glyphicon-film" aria-hidden="true"></span></a>
}

Open in new window


If you really want to continue with your new approach you'll need to explain what RecordingButtonField is supposed to contain. If it's what I think it is you're barking up the wrong tree with that approach.
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40493238
The @if is checked on the server and the button is only present if there is a RecordingUrl.
It looks like it's what you wanted.

What do you think is wrong in your first sample?
0
 

Author Closing Comment

by:RBS
ID: 40513087
Excellent - thanks - sorry - I missed this response and did not mark it earlier.

RBS
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Make the most of your online learning experience.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question