2011-12-24 14 views
1

questionを参照して、選択した値をcheckboxのデータベースに投稿する方法を学習しました。HttpPostを実行してデータベースにチェックボックスの選択値を格納する

のため、selectedの値はdebugになりません(下記のスナップショットを参照)。ここで

は私のコードです:

Model:

public class ProductModel 
{ 
    public string ProductId { get; set; } 
    public string ProductName { get; set; } 
    public bool Selected { get; set; } 
    public string[] CheckedColumn { get; set; } 
} 

View:

 @model IEnumerable<DemoApp.Models.ViewModels.ProductModel> 

    @{ 
       ViewBag.Title = "CheckView"; 
    } 

    <table> 
    <tr> 
     <th> 
      ProductId 
     </th> 
    <th> 
     ProductName 
    </th> 
    <th> 
     Selected 
    </th> 
     <th></th> 
    </tr> 

    @foreach (var item in Model) 
{ 
    <tr> 
     <td> 
     @Html.DisplayFor(modelItem => item.ProductId) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.ProductName) 
    </td> 
    <td> 
     @Html.CheckBoxFor(modelItem => item.Selected) 
    </td> 
    </tr> 
    } 

    </table> 
    @using (Html.BeginForm()) 
    { 
    <div> 
    <input type="submit" name="AddResult" value="CheckView"/> 
    </div> 
    } 

Controller:

[HttpGet] 
    public ActionResult CheckView() 
    { 
     DatabaseEntities db = new DatabaseEntities(); 

     var prodList = from b in db.SysUser3 
         select new ProductModel 
         { 
          ProductId = b.ProductId, 
          ProductName = b.ProductName, 
          Selected = b.SelectedProducts 
         }; 
     var p = prodList.ToList(); 
     return View(p); 

    } 

    [HttpPost] 
    public ActionResult CheckView(FormCollection collection) 
    { 
     try 
     { 
      ProductModel model = new ProductModel(); 
      // Get all the selected checkboxlist, do db insertion 
      model.CheckedColumn = collection["CheckView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 
      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View(); 
     } 

    } 

ここ

enter image description here

enter image description here

私は私のビューまたはコントローラで間違った何かをやっているプロジェクト

After selecting the values from below figureを実行している間、私が撮ったスナップショットのいくつかはありますか?誰か助けてくれますか?

答えて

6

Controller ACtionから何も渡されていないため、モデルはnullです。 CheckView ActionMethodはビューを生成するアクションでなければなりません。次に、あなたの "CheckView"ボタンはあなたの前のCheckViewのような別のHttpPostアクションを呼び出す必要があります。コメントに役立つかどうかを教えてください。

ビューのアクション結果方法:ボタンのクリック用

public ActionResult CheckView(ProductModel model) 
    { 
     return View("CheckView", model); 
    } 

アクション。

[HttpPost] 
    public ActionResult TestView(FormCollection collection) 
    { 
     try 
     { 
      ProductModel model = new ProductModel(); 
      // Get all the selected checkboxlist, do db insertion 
      model.CheckedColumn = collection["CheckView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 
      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View(); 
     } 

    } 

とビューでこのような何か:私はbobekに同意

@using (@Html.BeginForm("TestView", "Controller Name")) 
{ 
//all your input here 

// submit button here 
} 
+0

あなたが言及したコードの最初のブロックは、HttpGetメソッドですか? –

+0

質問の更新コードを確認してください。私はHttpGetメソッドも含めました –

+0

最初のものはhttpgetではありません。私のコードを試しましたか? –

1

。問題は、ポストアクションでモデルを再初期化していないことです。このような何か:

[HttpGet] 
    public ActionResult CheckView() 
    { 
     DatabaseEntities db = new DatabaseEntities(); 

     var prodList = from b in db.SysUser3 
         select new ProductModel 
         { 
          ProductId = b.ProductId, 
          ProductName = b.ProductName, 
          Selected = b.SelectedProducts 
         }; 
     var p = prodList.ToList(); 
     return View(p); 

    } 

    [HttpPost] 
    public ActionResult CheckView(FormCollection collection) 
    { 
     try 
     { 
      ProductModel model = new ProductModel(); 
      // Get all the selected checkboxlist, do db insertion 
      model.CheckedColumn = collection["CheckView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 
      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      ModelState.addModelError("", "There was an issue processing the results."); 
      DatabaseEntities db = new DatabaseEntities(); 

      var prodList = from b in db.SysUser3 
         select new ProductModel 
         { 
          ProductId = b.ProductId, 
          ProductName = b.ProductName, 
          Selected = b.SelectedProducts 
         }; 
      return View(prodList.ToList()); 
     } 

    } 

あなたはまた、おそらく重複したコードを避けるために、select文をリファクタリングしたいと思います。あなたがリストを扱っているとき、彼らは彼らの状態を維持しません。

1

これは既知のバグです。送信ボタンにjavascript関数をアタッチし、チェックボックスオプションの配列を繰り返し処理し、.selected = true;を使用します。チェックされているものについてその後、彼らは投稿を通じて送信されます。

関連する問題