Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Is AWS.RDS considered a part of "Big Data"? 4 66
word0 challenge 4 53
Magic Software info 18 103
Installed softare without GUID 6 54
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This is an explanation of a simple data model to help parse a JSON feed
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 …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

705 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

13 Experts available now in Live!

Get 1:1 Help Now