2017-05-01 19 views
0

ボタンを押すと、フィールドが空であるためモデルが無効であることが示されます。 モデルを見ると、すべてのフィールドが空またはnullです。この問題を解決する方法を誰もが知っている、感謝属性情報が見つかりません

ホームコントローラー

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> createOrderLine(Product models) 
    {  
     if (ModelState.IsValid) 
     { 
      db.OrderLines.Add(new OrderLine 
      { 
       productId = models.productId, 
       productColour = models.productColour, 
       productDescription = models.productDescription, 
       productName = models.productName, 
       productPrice = models.productPrice, 
       productStock = models.productStock, 
       //QuantityOrdered = quantityOrdered 
      }); 

      db.SaveChanges(); 

      return RedirectToAction("Index", "OrderLines"); 
     } 

     else return RedirectToAction("Index", "Home"); 
    } 

ホーム索引は -

@foreach (var product in Model) 
{ 

using (Html.BeginForm("createOrderLine", "Home", FormMethod.Post, new { 
@class = "form-horizontal", role = "form" })) 
{ 

<div class="row"> 
    <div class="blog col-md-6"> 
     <div> 
      <h1>Product Name</h1> 
      <h2>@product.productName</h2> 

      <div> 


       <h3>Product Description</h3> 

       <h6><i>@product.productDescription</i></h6> 

       <br /> 
       <h3>Product Price</h3> 

       <td>@product.productPrice </td> 
       <br /> 
       <h1>Product colour</h1> 

       <td>@product.productColour</td> 
       <div></div> 

       <br /> 
       <br />  

        @Html.AntiForgeryToken() 
       <div class="form-group"> 
        <div class="col-md-offset-2 col-md-10"> 
         <input type="submit" class="btn btn-buy" value="Add to cart" onclick="btnbuy_Click" /> 

        </div> 
       </div>     
      </div> 
     </div> 
     <br /> 
    </div> 
    </div> 
} 
} 
+0

フォームは成功したフォームコントロールの名前と値のペアのみを送信し、フォームコントロールは生成しません。しかし、1つのフォームしか提出できないので、なぜ複数の '

'要素を生成していますか? –

答えて

0

画面に製品情報を表示し、問題は、あなたのフィールドのどれもがリンクされていないということですこれらのモデルを表示するモデルは を修正するだけで、それぞれの製品のページに製品IDをリンクするhiddenforフィールドを追加することができます。

@Html.HiddenFor(product.Id) 

は、あなたがつもりパラメータとしてあなたのコントローラに渡されているIDで必要な情報の残りの部分を埋める

exemple:

public ActionResult createorderline(Product ProductToAdd) 
{ 
// add your product or do your treatment 
} 
0

私は非常にあなたがしようとしているのか理解していませんそうするが、ルイスは正しい。値を返す場合は、@Html.EditorFor()などの情報をサーバーに返す必要があります。

@Html.HiddenFor(product.Id)を使用して、Actionmethodのデータベースを呼び出して、製品に関する残りの情報を入手するか、JQueryを使用することもできます。

この場合、私は@Html.HiddenFor(product.Id)を使用してstaticヘルパーメソッドを使用してorderlineクラスをDBからマッピングします。あなたがビジネスModelViewModelからマップしないよう

public static Product GetProductById(int productId){ 
    return db.Products.FirstOrDefault(x => x.Id == productId); 
} 

しかし、再び、あなたは自分のコントローラ内のFirstOrDefault()行を追加することができます。

関連する問題