Asp.net mvc 如何显示';总和';cshtml中的内部文本框 @{ ViewBag.Title=“Index”; Layout=“~/Views/Shared/_Layout.cshtml”; Func Sum=(a,b)=>a+b; } //内桌 @总和(3,4)

Asp.net mvc 如何显示';总和';cshtml中的内部文本框 @{ ViewBag.Title=“Index”; Layout=“~/Views/Shared/_Layout.cshtml”; Func Sum=(a,b)=>a+b; } //内桌 @总和(3,4),asp.net-mvc,razor,Asp.net Mvc,Razor,这将输出正确的答案,尽管我希望它输出到一个可以调整的文本框中(以便数据可以发回)…我的尝试 @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; Func<int,int,int> Sum = (a, b) => a + b; } //inside table <td>@Sum(3,4)</td> @减去(@products.re

这将输出正确的答案,尽管我希望它输出到一个可以调整的文本框中(以便数据可以发回)…我的尝试

    @{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";

    Func<int,int,int> Sum = (a, b) => a + b;
}

//inside table
<td>@Sum(3,4)</td>

@减去(@products.reordlevel,@products.StockLevel)
这意味着输入元素是空的,不能有结束标记


理想情况下,我希望在文本框“+”%“-”后面有两个小按钮,当单击时,这些按钮将增加或减少文本框中的值……。

使用“值”属性设置文本输入字段的值

<td><input id="Name" name="Name" type="text" value="">                      
 @Minus(@products.ReorderLevel, @products.StockLevel) 
 </input>
 </td>
并从html中调用它:

<script type="text/JavaScript" src="/path/to/your/jquery.version.js"></script>
<script type="text/JavaScript">
     // Declare a function to increment a value
     var incrementField = function()
     {
         var newValue = 1 + parseInt($("#name").val());
         $("#name").val(newValue);
     };
     // Declare a function to decrement the value
     var decrementField = function()
     {
         var newValue = parseInt($("#name").val()) - 1;
         $("#name").val(newValue);
     };
</script>
最后,将ID添加到按钮并删除旧的单击处理程序

 // This creates a callback which called when the page is fully loaded
$(document).ready(function(){

    // Set the initial value of the textbox
    $("#name").val('0');

    // Create a click handler for your increment button
    $("#increaseButton").click(function(){
         var newValue = 1 + parseInt($("#name").val());
         $("#name").val(newValue);
    });
    // .. and your decrement button
    $("#decreaseButton").click(function(){
        var newValue = parseInt($("#name").val()) - 1;
        $("#name").val(newValue);
    });
});
+
- 

进入函数后,您可能不需要前缀为“@”的符号,例如“@(products.ReorderLevel,products.StockLevel)”,这太感谢了……当按下加号或减号按钮时,我是否使用某种形式的JS来增加或减少框中的总数修复了@DavidLeeps指出的错误,并添加了一种处理递增和递减的方法added jquery。我在每个按钮上使用相同的类,并将click事件与该按钮挂钩。然后我从按钮中提取id,并使用它找到正确的文本框
<button onclick="incrementField()">+</button>
<button onclick="decrementField()">-</button> 
 // This creates a callback which called when the page is fully loaded
$(document).ready(function(){

    // Set the initial value of the textbox
    $("#name").val('0');

    // Create a click handler for your increment button
    $("#increaseButton").click(function(){
         var newValue = 1 + parseInt($("#name").val());
         $("#name").val(newValue);
    });
    // .. and your decrement button
    $("#decreaseButton").click(function(){
        var newValue = parseInt($("#name").val()) - 1;
        $("#name").val(newValue);
    });
});
<button id="increaseButton">+</button>
<button id="decreaseButton">-</button>