错误处理:如果数据库行不存在,则显示空白输入页面

时间:2017-11-20 00:58:09

标签: c# asp.net .net

我有一个帐户设置页面供用户提交视频并为其提供标题以显示在他/她的个人资料中。提交后,视频将绑定到AccountInfoID并在我的dbo.Spotlight数据库中创建一行:

enter image description here

enter image description here

将视频聚光灯页面显示在上面的唯一方法是,如果您手动将YouTube视频输入到与accountinfoID绑定的数据库中,因为该页面可以拉动并显示填充输入框的信息。如果用户不存在任何行,则会引发错误:System.NullReferenceException: Object reference not set to an instance of an object.

我想知道我可以做什么错误处理来检查聚光灯数据库中是否找到了具有用户帐户的行,如果没有,仍然显示带有空白输入框的页面。 我在上面评论了我知道导致错误的行。

public ActionResult CreatorSpotlight()
{
    var model = new AccountSpotlightViewModel();
    var userID = User.Identity.GetUserId();
    var accountInfo = EntityDataAccess.GetAccountInfoByUserID(userID);
    if(accountInfo != null && accountInfo.CreatorFL == false)
    {
        return RedirectToAction("Spotlight", "Account");
    }
    //the next line is what causes the page to throw an error since it may not find the userid in the spotlight database
    model.Spotlight = EntityDataAccess.GetSpotlightByUserID(userID);
    model.Spotlight.AccountInfo = null;
    return View(model);
}

我通过ajax提交:

    function submitSpotlight()
    {

        var spotlight = new Object();
        spotlight.AlbumName = $("#youtube-title").val();
        spotlight.YouTubeURL = $("#video-spot-1").val();
        $.ajax
        ({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "@Url.Content("~/Account/SubmitSpotlight/")",
            data: JSON.stringify(spotlight),
            success: function(data)
            {
                if(data == true)
                {
                    location.reload();
                }
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                //TODO: Indicate Error
            }
        });
    }

这是与ajax请求绑定的JSON编码,该请求将值存储到dbo.spotlight

public JsonResult SubmitSpotlight(Spotlight spotlight)
        {
            try
            {
                var userID = User.Identity.GetUserId();
                spotlight.AccountInfoID = EntityDataAccess.GetAccountInfoByUserID(userID).AccountInfoID;
                if(!String.IsNullOrWhiteSpace(spotlight.YouTubeURL))
                {
                    var videoKey = spotlight.YouTubeURL.Replace("https://youtu.be/", "");
                    spotlight.EmbedYouTubeURL = "https://www.youtube.com/embed/" + videoKey + "?showinfo=0";
                    spotlight.ThumbnailURL = "http://img.youtube.com/vi/" + videoKey + "/maxresdefault.jpg";
                }
                var _spotlight = EntityDataAccess.GetSpotlightByUserID(userID);
                if(_spotlight == null)
                {
                    EntityDataAccess.InsertSpotlight(spotlight);
                }
                else
                {
                    spotlight.SpotlightID = _spotlight.SpotlightID;
                    EntityDataAccess.UpdateSpotlight(spotlight);
                }
                return Json(true);
            }
            catch(Exception ex)
            {
                return Json(ex);
            }
        }

更新了添加的GetspotlightbyuserID

  public static Spotlight GetSpotlightByUserID(string userID)
    {
        using(var Context = GetContext())
        {
            return Context.Spotlights.Include("AccountInfo").Where(x => x.AccountInfo.UserID == userID).FirstOrDefault();
        }
    }

1 个答案:

答案 0 :(得分:1)

错误可能来自此处的返回值

model.Spotlight = EntityDataAccess.GetSpotlightByUserID(userID);

在将model.Spotlight.AccountInfo设置为null之前,检查model.Spotlight的值是否为null,如下所示;

model.Spotlight = EntityDataAccess.GetSpotlightByUserID(userID);
if (model.Spotlight!=null){
    model.Spotlight.AccountInfo = null;
}   

return View(model);