如何将结果发送回同一视图?

时间:2016-09-08 08:58:18

标签: c# asp.net-mvc

如果我在我的视图中有这个:

@using (Html.BeginForm("Sum","Student"))
{
<p>
    Number1:
    @Html.TextBox("num1")
</p>
<P>
    Number2:
    @Html.TextBox("num2")
</P>
<input type="submit" value="Calculate"/>   
}

这在我的控制器中:

public void Sum(FormCollection values)
{
    var num11 = Convert.ToInt32(values["num1"]);
    var num21 = Convert.ToInt32(values["num2"]);
    int result = num11 + num21;  
    //How return result back to view and show it in a textbox      
}

如何将结果返回到视图并在textbox

中显示

修改:我按照建议尝试了以下代码,但然后将其重定向到另一个没有任何结果的视图。我想在将数据发送到sum方法的同一视图中显示结果:

public void Sum(FormCollection values)
{
     ....
     ViewBag.result = result;
}

....
 <input type="submit" value="Calculate"/>
    string str = (string)ViewBag.Res;
    @Html.TextBox("Result",str)

3 个答案:

答案 0 :(得分:1)

我建议您使用表单模型而不是命名字符串。它可以让你以更美丽的方式做更多的事情。

查看/测试/ Index.cshtml

@model MyForm
@using (Html.BeginForm("Index", "Test"))
{
    <div>@Html.TextBoxFor(m => m.Num1)</div>
    <div>@Html.TextBoxFor(m => m.Num2)</div>
    <div>@Html.TextBoxFor(m => m.Result)</div>
    <input type="submit" value="Calculate" />
}

Myform.cs

public class MyForm
{
    public int Num1 { get; set; }
    public int Num2 { get; set; }
    public long Result => Num1 + Num2;
}

TestController.cs

public class TestController : Controller
{
    [HttpGet]
    public ActionResult()
    {
        return View(new MyForm());
    }

    [HttpPost]
    public ActionResult Index(MyForm form)
    {
        return View(form);
    }
}

<强>已更新

看起来我们在这里谈论AJAX请求。虽然我认为在MVC中使用AjaxHelper是一种不好的做法(在具有层次结构结构的大型项目中),但在这种情况下它可能会正常工作。

@using (Ajax.BeginForm("Calculate", "Test", new AjaxOptions
{
    HttpMethod = "GET",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "Result"
}))
{
    <div>@Html.TextBox("num1")</div>
    <div>@Html.TextBox("num2")</div>
    <div id="Result"></div>
    <input type="submit" value="Calculate" />
}

和方法

public ActionResult Calculate(FormCollection values)
{
    var num1 = Convert.ToInt32(values["num1"]);
    var num2 = Convert.ToInt32(values["num2"]);
    return Content(num1 + num2);
}

不要忘记引用js

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

答案 1 :(得分:0)

public ActionResult Sum(FormCollection values)
{
    var num11 = Convert.ToInt32(values["num1"]);
    var num21 = Convert.ToInt32(values["num2"]);
    int result = num11 + num21;
    ViewBag.result =result; 
    //How return result back to view and show it in a textbox

     return View();      
}

在您的视图页面上使用此ViewBag.result

@Html.TextBox("result ", (string)ViewBag.result) 

答案 2 :(得分:0)

使用Jquery Ajax:
您的观点:

&#13;
&#13;
 

<body>

		<p>
			Number1:
			@Html.TextBox("num1")
		</p>
		<P>
			Number2:
			@Html.TextBox("num2")
		</P>
		<input type="submit" value="Calculate" id="submit"/>   
	
		<div id="result"></div>
	<!-- JS includes -->
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
	
	<script type="text/javascript">

	    $(function () {
	        $("#submit").click(function () {
	            $.ajax({
	                method: "POST",
	                dataType: "json",
	                url: '@Url.Action("Index","Test")',
	                data: { 'num1': $("#num1").val(), 'num2': $("#num2").val() },
	                success: function (result) {
	                    console.log(result);
	                    $("#result").text(result);
	                }
	            });
	        })
	    })

	</script>
</body>
&#13;
&#13;
&#13;

您的控制器:

&#13;
&#13;
public class TestController : Controller
{
           [HttpGet]
            public ActionResult Index()
            {
                return View();
            }


            [HttpPost]
            public JsonResult Index(string num1, string num2)
            {
                var num11 = Convert.ToInt32(num1);
                var num21 = Convert.ToInt32(num2);
                int result = num11 + num21;

                return Json(result);
            }
}
&#13;
&#13;
&#13;