MVC视图中的条件语句

时间:2014-08-28 20:15:15

标签: asp.net-mvc razor conditional-statements asp.net-mvc-views

我不明白为什么我的视图出现运行时错误。

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

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

Checking for NULL

EDITED

这是一个简单的CRUD视图。你认为我应该创建一个ViewModel并以这种方式检查null吗?通常,我总是使用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>

控制器:

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

2 个答案:

答案 0 :(得分:2)

您使用null或空参数调用@ Url.Content(Model.PicturePath)。您应该添加一些逻辑来检查Model.PicturePath是否有某些值。

@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)"
        type=@Model.FileType />
</video>
}

答案 1 :(得分:1)

也对您的模型执行空检查:

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

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