C# 如何使用ASP MVC在另一个文本框上设置总和值

C# 如何使用ASP MVC在另一个文本框上设置总和值,c#,asp.net,asp.net-mvc,model-view-controller,C#,Asp.net,Asp.net Mvc,Model View Controller,我正在尝试使用ASP.NET MVC将两个文本框的总和设置为另一个文本框,我知道这看起来很简单,但我不熟悉MVC。我只是通过使用一个@ViewBag.mensaje来实现这一点。这是我的代码: 型号: public class suma { public int n1 { get; set; } public int n2 { get; set; } public int total { get; set; } } 控制器: // GET: suma publ

我正在尝试使用ASP.NET MVC将两个文本框的总和设置为另一个文本框,我知道这看起来很简单,但我不熟悉MVC。我只是通过使用一个
@ViewBag.mensaje
来实现这一点。这是我的代码:

型号

public class suma
{
    public int n1 { get; set; }
    public int n2 { get; set; }
    public int total { get; set; }
}
控制器:

  // GET: suma

  public ActionResult Index()
  { 
      return View();
  }

  [HttpPost]
  public ActionResult Index(Models.suma model) 
  {
        int x = model.n1;
        int y = model.n2;
        int total = model.total;
        total = x + y;
        ViewBag.mensaje = (total).ToString();
        return View();
  }
查看:

@model bonito2.Models.suma

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

<h2>Index</h2>

@using (Html.BeginForm("Index", "suma", FormMethod.Post))
{

    @Html.TextBoxFor(model => model.n1)<br />
    @Html.TextBoxFor(model => model.n2)<br />
   <input type="submit" value="Submit" /> <br />

    @ViewBag.mensaje
}
@model bonito2.Models.suma
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
指数
@使用(Html.BeginForm(“Index”,“suma”,FormMethod.Post))
{
@Html.TextBoxFor(model=>model.n1)
@Html.TextBoxFor(model=>model.n2)

@ViewBag.mensaje }

我的代码可以正常工作,但我正在尝试这样做,或者在文本框上设置值,而不是在
@ViewBag.mensaje
中。谢谢。

您只需执行以下操作:

public class suma
{
    public int n1 { get; set; }
    public int n2 { get; set; }
    public int total => n1 + n2;
}
然后在html中:

@Html.TextBoxFor(model => model.total)

您可以简单地执行以下操作:

public class suma
{
    public int n1 { get; set; }
    public int n2 { get; set; }
    public int total => n1 + n2;
}
然后在html中:

@Html.TextBoxFor(model => model.total)
js代码

 $(document).ready(function() {
        //this calculates values automatically 
        sum();
        $("#n1, #n2").on("keydown keyup", function() {
            sum();
        });
    });
function sum() {
            var num1 = document.getElementById('n1').value;
            var num2 = document.getElementById('n2').value;
            var result = parseInt(num1) + parseInt(num2);
            if (!isNaN(result)) {
                document.getElementById('total').value = result;
            }
        }
并将其添加到html中

<input type="text" name="total" id="total" readonly />

js代码

 $(document).ready(function() {
        //this calculates values automatically 
        sum();
        $("#n1, #n2").on("keydown keyup", function() {
            sum();
        });
    });
function sum() {
            var num1 = document.getElementById('n1').value;
            var num2 = document.getElementById('n2').value;
            var result = parseInt(num1) + parseInt(num2);
            if (!isNaN(result)) {
                document.getElementById('total').value = result;
            }
        }
并将其添加到html中

<input type="text" name="total" id="total" readonly />


例如,向模型中添加
Total
属性,在
get
方法中写入
返回n1+n2
(或计算内部ActionMethod)。例如,向模型中添加
Total
属性,在
get
方法中写入
返回n1+n2
(或计算内部ActionMethod)。我尝试了这个方法,但没有成功,
@Html.TextBoxFor(model=>model.total)
创建了一个新的文本框,但sum不起作用。单击按钮后,它只是显示为空。我遗漏了什么?我尝试了这个,但没有成功,
@Html.TextBoxFor(model=>model.total)
创建了一个新的文本框,但是sum没有成功。单击按钮后,它只是显示为空。我错过了什么?