2017-06-12 120 views
-1

enter image description here任何機構都可以幫助解決此代碼中的問題嗎? 遺漏的類型錯誤:無法讀取屬性「值」空 的我在PartialView有這樣的代碼Uncaught TypeError:無法讀取null值的屬性值,ASP.NET MVC

<table class="table"> 
     <tr> 

      <th> 
       Products 

      </th> 
      <th> 
       Quantity 

      </th> 
      <th></th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      <tr> 

       <td> 
        @Html.DisplayFor(modelItem => item.Product) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.Quantity) 
       </td> 

       <td> 
      <input type='text' name='[email protected](item.Product)' id='[email protected](item.ProductId)'/> 

       <Button class="btn btn-success btn-xs glyphicon glyphicon-plus" name="add" onclick='javascript: document.getElementById("qty").value++;' value='+' /> 
       <Button class="btn btn-warning btn-xs glyphicon glyphicon-minus" name="subtract" onclick='javascript: subtractQty([email protected](item.ProductId));' value='-' /> 
       <button type="button" class="btn btn-primary" onclick="update();">Ok</button> 
      </td> 

      </tr> 
     } 

    </table> 

And JavaScript in my Index.cshtml: 


    function subtractQty(name) { 
      if (document.getElementById(name).value - 1 < 0) 
       return; 
      else 
       document.getElementById(name).value--; 
     } 

我'試圖把文本框在我@foreach PartialView頁面 預先感謝您的傢伙!

回答

0

爲您添加按鈕onclick事件是:

onclick='javascript: document.getElementById("qty").value++;

的「數量」應爲「數量@(item.ProductId)」,以便它使用的值在循環。

您減法按鈕onclick事件是:

onclick='javascript: subtractQty([email protected](item.ProductId));' 

到subtractQty的參數應該用引號括起來。

這裏是沒有模板變量

<script type="text/javascript"> 
    function subtractQty(name) { 
      if (document.getElementById(name).value - 1 < 0) 
       return; 
      else 
       document.getElementById(name).value--; 
     } 
</script> 
<table class="table"> 
     <tr> 

      <th> 
       Products 

      </th> 
      <th> 
       Quantity 

      </th> 
      <th></th> 
     </tr> 

      <tr><td> 
      <input type='text' name='qty' id='qty' value="0"/> 

       <Button class="btn btn-success btn-xs glyphicon glyphicon-plus" name="add" onclick='javascript: document.getElementById("qty").value++;' value='+' /> 
       <Button class="btn btn-warning btn-xs glyphicon glyphicon-minus" name="subtract" onclick='javascript: subtractQty("qty");' value='-' /> 
       <button type="button" class="btn btn-primary" onclick="update();">Ok</button> 
      </td> 

      </tr> 

    </table> 
+0

拉胡爾VaidyaBut每次點擊進入到第一個文本框becouse所有textbos具有相同名稱的工作示例。請再次跪下我的照片,已更新照片 –

+0

我已更新照片 –

相關問題