-1

次の問題があります。MVC:DropdownListForエラー "ラムダ式がデリゲート型ではないため 'string'型に変換できません"

私はUsersと呼ばれるテーブルを持っていて、別のテーブルDepartmentがあります。

ユーザーフォームのユーザーとして自分自身を作成し​​ようとしたときに、ユーザーがDepartmentフィールドに表示されると、Departmentテーブルから利用可能なすべてのDepartmentタイトルがドロップダウンリストに表示されます。その理由:部門テーブルは他の操作に使用されるため、Userdataとリンクしてはいけません。したがって、Userdataにはテーブルの部門名だけが含まれていれば十分です。

私のコントローラは次のようになります。

私のHTMLセクションで
public ActionResult UserCreate() 
{ 
    ViewBag.AppDataDepartment = new SelectList(database.department, "department_title", "department_title"); 
    return View(); 
} 

[HttpPost] 
public ActionResult UserCreate(Users user) 
{ 
    if (user.UserID == "" || user.UserID == null) 
    { 
     ModelState.AddModelError(string.Empty, "UserID cannot be blank"); 
    } 
    try 
    { 
     if (ModelState.IsValid) 
     { 
      List<string> results = database.Database.SqlQuery<String>(string.Format("SELECT UserID FROM USERS WHERE UserID = '{0}'", user.UID)).ToList(); 
      bool _userExistsInTable = (results.Count > 0); 
      Users _user = null; 
      if (_userExistsInTable) 
      { 
       _user = database.Users.Where(p => p.UserID == user.UserID).FirstOrDefault(); 
       if (_user != null) 
       { 
        if(_user.active == true) 
        { 
          ModelState.AddModelError(string.Empty, "USER already exists!"); 
        } 
        else 
        { 
         database.Entry(_user).Entity.active = true; 
         database.Entry(_user).Entity.Last_Modified = System.DateTime.Now; 
         database.Entry(_user).State = EntityState.Modified; 
         database.SaveChanges(); 
         return RedirectToAction("Index"); 
        } 
       } 
      } 
      else 
      { 
       _user = new Users(); 
       _user.UserID = user.UserID; 
       _user.lastname = user.lastname; 
       _user.firstname = user.firstname; 
       _user.mail = user.mail; 
       _user.department = user.department; 
       _user.user_image = user.user_image; 
       _user.image_path = user.image_path; 
       if (ModelState.IsValid) 
       { 
        _user.active = true; 
        _user.Last_Modified = System.DateTime.Now; 
        database.Users.Add(_user); 
        database.SaveChanges(); 
        ViewBag.AppDataDepartment = new SelectList(database.department, "department_title", "department_title"); 
        return RedirectToAction("Index"); 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     //return base.ShowError(ex); 
    } 
    return View(user); 
} 

<div class="row"> 
    @Html.LabelFor(model => model.UserID, "UserID", new { @class = "col-sm-2 control-label" }) 
    <div class="col-md-4"> 
     @Html.TextBoxFor(model => model.UserID, new { @class = "col-md-4 control-label form-control", @id = "inputEmail3" }) 
    </div> 

    @Html.LabelFor(model => model.department, "Department", new { @class = "col-sm-2 control-label" }) 
    <div class="col-md-4"> 
     @Html.DropDownList(model => model.department, (SelectList) ViewBag.AppDataDepartment, htmlAttributes: new { @class = "form-control" }) 
    </div> 
</div> 

そしてテーブルから最後に私の部署クラス:

[Table("department")] 
public partial class department 
{ 
    [Key] 
    public int departmentid { get; set; } 
    [Required] 
    public string department_title { get; set; } 
    public string subdepartment { get; set; } 
} 

しかし、私はできません私はエラーが発生するので、コンパイルしてください:

なぜこれは機能しませんか?どうすれば修正できますか?

私はすでにこのSolutionProposalを見ましたが、モデル、LinqとData.Entityが既に参照されているため、これは役に立たなかった。

+3

'@ Html.DropDownListFor()'ではない '@ Html.DropDownList()' –

+0

@StephenMueckeのおかげで、その後私は、次のエラーメッセージが表示されます。 エラーCS1928 \tは 'HtmlHelperの'「DropDownListForの定義が含まれていません。 'と最適な拡張メソッドオーバーロード' SelectExtensions.DropDownListFor (HtmlHelper 、式>、IEnumerable 、オブジェクト) 'が無効な引数を持っています – Azeristar

+0

不思議なことは何でもできます。あなたが縛っているモデルは何ですか? (あなたが示したのは 'class department'ですが、' department'という名前のプロパティは含まれていません) –

答えて

2

それはしかし、あなたが取り組むべきあなたのコードと、他の多くの問題があるあなたがDropDownList()を使用した場合、それは

@Html.DropDownList("department", (SelectList)ViewBag.AppDataDepartment, new { @class = "form-control" }) 

なりませんDropDownList()

@Html.DropDownListFor(m => m.department, (SelectList)ViewBag.AppDataDepartment, new { @class = "form-control" }) 

DropDownListFor()にする必要があります。

  1. ビューモデルを使用する必要がありますので、URLは、../User/Create、ないUser/UserCreate
  2. あなたの編集データとなるように、お使いのコントローラが編集しているとき( データモデルを使用していないUserController及び方法Create() する必要がありますデータ) - What is ViewModel in MVC?、 を参照し、ビューモデルにはプロパティーが含まれている必要があります。 IEnumerable<SelectListItem> DepartmentList - 典型的な例は、this question/answerに示されている です。
  3. あなたUsersテーブルはDepartmentID、 ないその名前を格納しなければならない、と DepartmentsテーブルにFK関係があるはずです。
  4. UserIDのためのあなたのビューモデルのプロパティが[Required] 属性を持つ必要があり、あなたがボーテクライアントとサーバー側の検証(DepartmentIDプロパティの同上)を取得するようにビューが@Html.ValidationMesageFor(m => m.UserID)を含める必要があります。また あなたは、クライアント側の検証を取得しているので UserIDプロパティに適用RemoteAttributeを検討すべきである - How to: Implement Remote Validation in ASP.NET MVC

を参照してください。そして最後に、ほとんど何もPOSTメソッドはあまり意味を作っているあなたではありません。ユーザーが既に存在するかどうかを確認するために、データベースを2回呼び出します。 ModelState.IsValidを複数回チェックしています。 user_imageimage_pathのプロパティは、画像をアップロードする必要があることを示唆していますが、保存する場所はありません。 ViewBag.AppDataDepartmentを割り当て、すぐにIndex()メソッドにリダイレクトします。あなたのコードでは、アーカイブされた既存のユーザーはCreate()方法に移動する必要がある理由のようなもの(ファイルアップロードの問題を無視して)その不明確ものの

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(UserVM model) 
{ 
    // Return early 
    if (!ModelState.IsValid) 
    { 
     // You will enter this automatically of UserID is null 
     model.DepartmentList = ... // assign the SelectList 
     return View(model); 
    } 
    // One database call to check if the user exists 
    User user = database.Users.Where(p => p.UserID == model.UserID).FirstOrDefault(); 
    if (user != null && !user.active) 
    { 
     user.active = true; 
     // Save and redirect 
    } 
    else if (user != null) 
    { 
     ModelState.AddModelError(string.Empty, "USER already exists!"); 
     model.DepartmentList = ... // assign the SelectList 
     return View(model); 
    } 
    user = new User 
    { 
     UserID = model.UserID, 
     lastname = model.lastname, 
     .... // set other properties of User 
     Last_Modified = System.DateTime.Now 
    } 
    // Save and redirect 
} 

を見なければならない(と全体を記入するフォームを提示すること彼らがすでに以前に入力した詳細の多く)。以前にアーカイブされたユーザーをアクティブ化するための別の方法が必要です。

+0

実際これは管理者のユーザーコントロールです。私は、役割、権限、およびユーザーを持つRBACモデルを実装しました。それらはすべてコントローラ管理者にまとめられています。 CreateRoleとCreatePermissionも持っているので、CreateUserのようなアクション名を持つのはこのためです。あなたが投稿したリンクはとても役に立ちます。 – Azeristar

+0

次に[エリア](http://www.codeguru.com/csharp/net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm)を使用する必要があります –

+0

ありがとう、これは非常に論理的に聞こえる。私は今日それを実装しようとします:)ヘルプとリンクをありがとう。 – Azeristar

関連する問題