JoshYates1980
JoshYates1980

Reputation: 3626

Conditional statement in MVC View

I don't understand why my view has a runtime error.

    @if (Model.PicturePath != null)
    {
    <dt>
        @Html.DisplayNameFor(model => model.PicturePath)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.PicturePath)
    </dd>
    }

Checking for NULL

EDITED

This is a simple CRUD view. Do you think I should just create a ViewModel and check for null that way? Normally, I always use a ViewModel.

@model PTSPortal.Models.File

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
<h4>File</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.FileName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.FileName)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.FileSize)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.FileSize)
    </dd>
    @if (Model.FilePath != null)
    { 
    <dt>
        @Html.DisplayNameFor(model => model.FilePath)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.FilePath)
    </dd>
    }

    <dt>
        @Html.DisplayNameFor(model => model.CreateDate)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.CreateDate)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.FileType)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.FileType)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.UserId)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.UserId)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.IsPublic)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.IsPublic)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.FriendlyName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.FriendlyName)
    </dd>
         @if (Model.PicturePath != null)
          {
           <dt>
             @Html.DisplayNameFor(model => model.PicturePath)
           </dt>

           <dd>
              @Html.DisplayFor(model => model.PicturePath)
           </dd>
           }
 </dl>
</div>
<br /><br />
l.FileType />
</video><video id="video1" class="video-js vjs-default-skin" controls preload="auto"
   width="640" height="264" poster="@Url.Content(Model.PicturePath)"
   data-setup='{"example_option":true}'>
<source src="@Url.Content(Model.FilePath)"
        type=@Mode
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.FileId }) |
@Html.ActionLink("Back to List", "Index")
</p>

The controller:

   public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        PTSPortal.Models.File file = db.Files.Find(id);
        if (file == null)
        {
            return HttpNotFound();
        }
        return View(file);
    }

Upvotes: 2

Views: 3504

Answers (2)

ntl
ntl

Reputation: 1299

You are calling @Url.Content(Model.PicturePath) with a null or empty argument. You should add some logic to check if Model.PicturePath has some value.

@if (Model.PicturePath != null && Model.FilePath != null){

<video id="video1" class="video-js vjs-default-skin" controls preload="auto"
   width="640" height="264" poster="@Url.Content(Model.PicturePath)"
   data-setup='{"example_option":true}'>
<source src="@Url.Content(Model.FilePath)"
        [email protected] />
</video>
}

Upvotes: 2

Troy Carlson
Troy Carlson

Reputation: 3111

Perform a null check on your model as well:

@if (Model != null && Model.PicturePath != null)
{
    <dt>
        @Html.DisplayNameFor(model => model.PicturePath)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.PicturePath)
    </dd>
}

Upvotes: 1

Related Questions