2016-02-22 29 views
0

我有問題從數據庫獲取數據時,當我在文本框中鍵入id我得到一個彈出消息System.NullReferenceException。我正在嘗試使用記錄中的詳細信息自動填充文本框。 enter image description here數據庫實體問題不斷收到錯誤消息System.NullReferenceException

UserRepository.cs

using System.Collections.Generic; 
using System.Linq; 
using SampleMvc.Domain; 

namespace SampleMvc.Repository 
{ 
    public class UserRepository : IUserRepository 
    { 
     #region IUserRepository Members 

     private UsersDBEntities UsersDBEntities; 

     public User GetUser(string lanId) 
     { 
      return UsersDBEntities.Users.Where(user => user.LanId == lanId).FirstOrDefault(); 
     } 


     #endregion 
    } 
} 

IUserRepository.cs

using SampleMvc.Domain; 

namespace SampleMvc.Repository 
{ 
    public interface IUserRepository 
    { 
     User GetUser(string LanId); 
    } 
} 

UserDetails.cshtml

@model SampleMvc.Models.UserModel 
@{ 
    ViewBag.Title = "UserDetails"; 
} 

<link href="../../Content/Site.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script> 

<script type="text/javascript"> 

    $(document).ready(function() { 
     $('#LanId').change(function() { 
      populateUserDetails(); 
     }); 

    }); 

    function populateUserDetails() { 
     var user = {}; 
     user.LanId = $("#LanId").val(); 
     $.getJSON("PopulateDetails", user, updateFields); 
     //$.post("PopulateDetails", user, updateFields, 'json'); 
    }; 

    updateFields = function (data) { 
     $("#LastName").val(data.lastName); 
     $("#FirstName").val(data.FirstName); 
     $("#Message").html(data.Message); 
    }; 
</script> 



@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal container"> 


     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.LanId, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.LanId, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.LanId, "", new { @class = "text-danger" }) 
       <br /> 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10 "> 
       @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.lastName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.lastName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.lastName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 




     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-primary" /> 
      </div> 
     </div> 
    </div> 

} 

UserController.cs

using System; 
using System.Web.Mvc; 
using SampleMvc.Domain; 
using SampleMvc.Models; 
using SampleMvc.Repository; 

namespace SampleMvc.Controllers 
{ 
    public class UserController : Controller 
    { 
     private readonly IUserRepository _userRepository; 

     public UserController() 
     { 
      _userRepository = new UserRepository(); 
     } 

     public UserController(IUserRepository userRepository) 
     { 
      _userRepository = userRepository; 
     } 

     public ActionResult UserDetails() 
     { 
      UserModel model = new UserModel(); 
      return View(model); 
     } 

     public JsonResult PopulateDetails(UserModel model) 
     { 
      UserResultModel userResultModel = new UserResultModel(); 
      if (String.IsNullOrEmpty(model.LanId)) 
      { 
       userResultModel.Message = "LanId can not be blank"; 
       return Json(userResultModel,JsonRequestBehavior.AllowGet); 
      } 

      User user = _userRepository.GetUser(model.LanId); 

      if (user == null) 
      { 
       userResultModel.Message = String.Format("No LanId found for {0}", model.LanId); 
       return Json(userResultModel,JsonRequestBehavior.AllowGet); 
      } 
      userResultModel.lastName = user.lastName; 
      userResultModel.FirstName = user.FirstName; 

      userResultModel.Message = String.Empty; //success message is empty in this case 

      return Json(userResultModel, JsonRequestBehavior.AllowGet); 
     } 
    } 

    public class UserResultModel 
    { 
     public string lastName { get; set; } 
     public string FirstName { get; set; } 
     public string Message { get; set; } 
    } 
} 
+0

你不實際設置UserDBEntities任何東西,你只是聲明爲私有。 – Duston

+0

我很困惑如何修復代碼。我需要改變我的回報嗎? – Donny

+0

我不建議您使用默認構造函數來設置默認存儲庫。在這種情況下,你在DI中失去意義並且可能有意想不到的行爲 –

回答

0

在你的倉庫類:

private UsersDBEntities UsersDBEntities; 

public UserRepository() 
{ 
    if (UserDBEntities == null) 
    { 
    UserDBEntities = new UserDBEntities(); 
    } 
} 

雖然我建議你使用一個變量名是不一樣的類名。

下面是從我的代碼示例:

private SurveyContext db = null; 

    public Survey() 
    { 
     if (db == null) 
     { 
      db = new SurveyContext(); 
     } 
    } 
+0

那麼,我在我的回報 – Donny

+0

我得到它的工作 – Donny

相關問題