【问题标题】:How to Pass Values from TextArea to another view on ASP .NET MVC 4如何将值从 TextArea 传递到 ASP .NET MVC 4 上的另一个视图
【发布时间】:2015-01-08 18:10:52
【问题描述】:

我对所有这些 MVC4 和 ASP .NET 都是新手,我试图制作一个简单的表单来获取文本,然后将其提交到另一个视图并在那里打印,我已经搜索了教程和数据,但仍然不知道如何这样做,我制作了表单视图并获取了要转到控制器的值,这是我的代码:

控制器:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication10.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Message = "Modifique esta plantilla para poner en marcha su aplicación ASP.NET MVC.";

            return View();

       }
        [HttpPost]
        public ActionResult Index(string text) {

            return RedirectToAction("About", "Home");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Página de descripción de la aplicación.";

            return View();
        }


}

查看

    @{
    ViewBag.Title = "Página principal";
}

<h3>Formulario</h3>

    @using (Html.BeginForm())

    {
        @Html.Label("Escribe lo que quieras")<br />
        @Html.TextArea("text")<br />

        <button type="submit">Enviar</button>

    }

这就是我目前所拥有的。

【问题讨论】:

  • 你需要一个“for”助手(除了显示)来将你的数据绑定到模型

标签: c# asp.net asp.net-mvc forms asp.net-mvc-4


【解决方案1】:

获取文本,然后将其提交到另一个视图并在那里打印

您可以使用TempDataSession。对于您的方案,TempData 可能是最佳选择。

...

[HttpPost]
public ActionResult Index(string text)
{
    TempData["Text"] = text;
    return RedirectToAction("About", "Home");
}

public ActionResult About()
{
    ViewBag.Message = TempData["Text"];
    return View();
}

【讨论】:

    猜你喜欢
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多