Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-11-29
3
Medium Priority
?
109 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

972 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