2

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

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

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

检查 NULL

已编辑

这是一个简单的 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);
    }
4

2 回答 2

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>
}
于 2014-08-28T21:07:40.103 回答
1

对您的模型执行空检查:

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

    <dd>
        @Html.DisplayFor(model => model.PicturePath)
    </dd>
}
于 2014-08-28T20:51:11.467 回答