Conditional Statement in MVC View

advertisements

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>
    }

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)"
        [email protected]
<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);
    }


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>
}