Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# DataGridView_RowsAdded event not firing 1 74
Path to Python 9 118
c++ reading data from file into two dimensional array 3 107
iSeries email authority 6 56
This article will show, step by step, how to integrate R code into a R Sweave document
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

785 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