2017-07-29 11 views
0

コントローラーでmetod createにデータを渡す際に以下の問題があります。メソッドの まずIディスプレイビュー(GET)ビューからメソッドへのデータの受け渡しコントローラでの作成

public ActionResult Create(string id) 
     { 
      RolesUser RolesUser = new RolesUser(id,repository.GetUserById(id).Roles, repository.GetRoles().ToList());   
      return View(RolesUser); 
     } 

を作成し、私はそれが私を示して提出実行するとそれは、私にCreate.cshtmlの私のビューの基本的な情報が表示され、次のViewModel

public class RolesUser 
    { 
     public RolesUser() 
     { 

     } 

     public RolesUser(string id,ICollection<IdentityUserRole> userRoles,List<IdentityRole> Roles) 
     { 
      userId = id; 
      this.Roles = GetNewRolesForUser(userRoles.ToList(), Roles.ToDictionary(x => x.Id, x => x.Name)).ConvertAll(
       role => 
       { 
        return new SelectListItem() 
        { 
         Text = role.ToString(), 
         Value = role.ToString(), 
         Selected = false 
        }; 
       }); 
     } 

     public IEnumerable<SelectListItem> Roles { get; set; } 

     public string userId { get; set; } 

     private List<string> GetNewRolesForUser(List<IdentityUserRole> UserRoles,Dictionary<string,string> Roles) 
     { 
      List<string> AvaiableRoles = new List<string>(); 
      List<string> IdUserRoles = new List<string>(); 
      UserRoles.ForEach(item => IdUserRoles.Add(item.RoleId)); 

      foreach(KeyValuePair<string,string> Role in Roles) 
      { 
       if (!IdUserRoles.Contains(Role.Key)) 
       { 
        AvaiableRoles.Add(Role.Value); 
       } 
      } 
      return AvaiableRoles; 
     } 

    } 

を使用次のエラー オブジェクト参照がオブジェクトのインスタンスに設定されていません。

metod(POST)を作成するにはCreate.cshtml

@model Repository.ViewModels.RolesUser 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Dodaj poziom dostępu</h2> 


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

    <div class="form-horizontal"> 
     <h4>@Model.userId</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

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

     <div class="form-group"> 
     @Html.Label("Poziomy dostępu", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(m => m.userId, Model.Roles) 
     </div> 
     </div> 



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

のコードこの

[HttpPost] 
     [ValidateAntiForgeryToken] 
     [Authorize(Roles ="Admin")] 
     public ActionResult Create([Bind(Include ="userId")] RolesUser user) 
     { 
      if (ModelState.IsValid) 
      { 
       try 
       { 

        repository.AssignToRole(user.userId, "Klient"); 
        repository.SaveChanges(); 
       } 
       catch 
       { 
        ViewBag.exception = true; 
        return View(); 
       }  
      } 
      ViewBag.exception = false; 
      return RedirectToAction("Index"); 
     } 

のように見えるのviewmodelメソッドに渡していないように見えますので、私はnullの例外があります。しかし、私はGET metodが適切に表示されれば、理由を理解できません。

+0

「NullReferenceException」はどこにありますか? (POSTメソッドの 'return View();'行を返すと確かに発生します) –

答えて

0

user.userIdがヌルであるため、NullReferenceExceptionはrepository.AssignToRole()メソッドでヒットする可能性があります。フォーム内のどこかにuserIdの隠し入力を追加して、userId値がCreateアクションのパラメータオブジェクトに送信されるようにする必要があります。 @Html.AntiForgeryToken()の後に追加すると、フォームの開始は次のようになります

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(model => model.userId) 

    ... 
+0

これはうまく動作しますが、同じ問題がdropdowlistforにあります。ここで私は役割を選択し、このファイルを隠すことはできません。 –

関連する問題