Solved

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

Posted on 2014-11-29
3
90 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 80
Explain Unit of Work pattern 2 57
Linq help 1 25
T-SQL:  Sigh---Boy, this is fun.... 12 28
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now