2017-06-16 3 views
0

これで、.net core mcvを学習しようとしています.MySQLからデータをMyフォームにマッピングする際に問題が発生しています。フォームを単一のテキストボックスとして単一のボタンで作成し、フォームの外にある(同じページにある)他のフィールドは、うまく動作します。フォーム内のすべてのフィールドを含めると、データは取得されますが、ページには表示されません。私は、データの更新として複数の送信ボタンの1つをコーディングすることさえ行っています。最初のテキストボックスを使用してデータベースからアイテムを取得します(ただし、テキストボックスにはマッピングされません)。次に、2番目のテキストボックス(既存のデータがあるはずですが空です)に、データベース内で更新する情報、そのテキストボックスの送信ボタンをクリックすると、データベースが更新されます(ただし、ビュー内のテキストボックスは空白のままです)。マルチボタンフォームは、データをテキストボックスにマップしません.net core 1.1

マイモデル:

using System; 

namespace DbTest.Models 
{ 
public class ProductInventory 
    { 
     public string Field1 { get; set; } 
     public string Field2 { get; set; } 
     public string Field3 { get; set; } 
     public int Field4 { get; set; } 
    } 
} 

私のコントローラ:

using System; 
using Microsoft.AspNetCore.Mvc; 
using MySql.Data.MySqlClient; 
using Microsoft.AspNetCore.Authorization; 

using DbTest.Models; 

namespace DbTest.Controllers 
{ 
    public class InventoryController : Controller 
    { 
//  [Authorize] 
     public IActionResult Index() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public IActionResult ProcessForm(string button, ProductInventory p) 
     { 
      IActionResult toDo = null; 

      if (button == "Button1") 
      { 
       toDo = GetItem(p); 
      } 
      if (button == "Button2") 
      { 
       toDo = UpdateField2(p); 
      } 
      if (button == "Button3") 
      { 
       toDo = UpdateField3(p); 
      } 
      if (button == "Button4") 
      { 
       toDo = UpdateField4(p); 
      } 

      return toDo; 
     } 

//  [HttpPost] 
     public IActionResult GetItem(ProductInventory p) 
     { 
    //CODE SNIP - DATABASE QUERY, IT ALL WORKS, SO WHY BOTHER YOU WITH THE DETAILS? 
     return View("Index", p); 
     } 

     public IActionResult UpdateField2(ProductInventory p) 
     { 
     //CODE SNIP - DATABASE UPDATE, ALL WORKS, NOTHING TO SEE HERE 
     return View("Index", p); 
     } 
    } 
} 

そして最後に、私の意見:私はButton1の後にフォームを閉じる場合

@model DbTest.Models.ProductInventory 

@{ 
    ViewData["Title"] = "Inventory Page"; 
} 

@using (Html.BeginForm("ProcessForm", "Inventory", FormMethod.Post)) 
{ 
     <div> 
      Search Item (Field 1): 
      @Html.TextBoxFor(model => model.Field1) 
      <input type="submit" name="button" value="Button1" /> 
     </div> 
     <div> 
      Field 2: 
      @Html.TextBoxFor(model => model.Field2) 
      <input type="submit" name="button" value="Button2" /> 
     </div> 
     <div> 
      Field 3: 
      @Html.TextBoxFor(model => model.Field3) 
      <input type="submit" name="button" value="Button3" /> 
     </div> 
     <div> 
      Field 4: 
      @Html.TextBoxFor(model => model.Field4) 
      <input type="submit" name="button" value="Button4" /> 
     </div> 
} 

は、繰り返しに:

@using (Html.BeginForm("ProcessForm", "Inventory", FormMethod.Post)) 
{ 
     <div> 
      Search Item (Field 1): 
      @Html.TextBoxFor(model => model.Field1) 
      <input type="submit" name="button" value="Button1" /> 
     </div> 
} 
     <div> 
      Field 2: 
//etc. 

マッピングは機能しますが、フォームの最初のフィールドとボタンのみが機能します。 4つのフィールドとボタンのすべてのフォームで、マッピングは機能しませんが、2番目のボタンのコーディングでButton2をクリックするとデータベースが更新されます。

ここで間違ったことを誰かが説明できますか?

ありがとうございます!

答えて

0

まず、ASP.NETコアでhtmlヘルパーを使用しないでください。これらの作業はうまくいかないものの、ベストプラクティスではありません。可能であれば、代わりにtag helpersを使用してください。さらに、ビューモデルとしてDBモデルを使用しないでください。

あなたのIndexアクションについて:あなたのビューにビューモデルを渡すことを忘れました。

ProcessFormアクションでは、IActionResultをインスタンス化し、それに(アクション)関数を割り当てます。それをしないでください。代わりにreturn RedirectToAction("ActionName");を使用してください。 あなたのケースでは、ProcessFormアクション内またはIActionResultを返さない関数内のDB更新を処理します。

結論として、ASP.NET Coreのドキュメントを読んで、まだ動作しないかどうか再度確認することをお勧めします。 thisと読むことをお勧めします。

関連する問題