Uncaught TypeError:无法读取null值的属性值,ASP.NET MVC
问题描述:
任何机构都可以帮助解决此代码中的问题吗? 遗漏的类型错误:无法读取属性“值”空 的我在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页面 预先感谢您的家伙!
答
为您添加按钮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>
拉胡尔VaidyaBut每次点击进入到第一个文本框becouse所有textbos具有相同名称的工作示例。请再次跪下我的照片,已更新照片 –
我已更新照片 –