如何将输入类型文本值传递给 View MVC 中的 TempData

How to pass Input type text value to TempData in View MVC

如何将输入类型文本值传递给 TempData

<input type='text' name='APP_COMMENT' />
@{TempData["APP"] = //This's where I want the input value;}

编辑:

这是我需要从视图发送到控制器(不是控制器来查看)的操作

public ActionResult Approved()
    {
        Entities1 db = new Entities1();
        string y=(TempData["APP"]).ToString(); //TempData is send from the View to Controller
        decimal sl = (decimal)TempData["rt"];
        var sp = db.TB_RST_SVCHDR.Where(x => x.REQ_NO == sl);

        foreach (var p in sp)
        {
            if (p.STATUS == "N")
            {
                p.STATUS = "A1";
                p.APP1_COMMENT = y; //Here's where the TempData is saved 
                p.APP1_DATEACTION = DateTime.Now;
                p.APPROVER1 = "";
                var s = p.APPROVER2;
                //var ss = p.APPROVER2; 
                var s2 = p.REQUESTOR_EMPNAME;
                TempData["email-act2"] = s2;
                //SendEmail(s, ss);
                SendEmail(s);
                db.SaveChanges();
                return View("~/Views/Home/Index.cshtml");
            }
        }
        //return View();
        return View("~/Views/Home/Index.cshtml");
    }

除非您想要在输入更改时更新 TempData["APP"] 的动态内容,否则您可以做的是在发布该表单时分配值。所以,你做了这种性质的事情。

public async Task<IActionResult> OnPostAsync(Model model) 
{ 
    model.YourField = TempData["APP"] 
} 

但是您的问题看起来更像是一个 Post 动作。在这种情况下,您应该考虑阅读如何使用表单 post 数据。使用 this.

 @using (Html.BeginForm("Approved", "Controllername", FormMethod.Post))
 {
        <input type="text" name="data"> 
        <input type="submit">
 }

并像这样设置控制器动作:

[HttpPost]
public ActionResult Approved(string data)
{
    Entities1 db = new Entities1();
    string y= data;
    decimal sl = (decimal)TempData["rt"];
    var sp = db.TB_RST_SVCHDR.Where(x => x.REQ_NO == sl);

    foreach (var p in sp)
    {
        if (p.STATUS == "N")
        {
            p.STATUS = "A1";
            p.APP1_COMMENT = y; //Here's where the TempData is saved 
            p.APP1_DATEACTION = DateTime.Now;
            p.APPROVER1 = "";
            var s = p.APPROVER2;
            //var ss = p.APPROVER2; 
            var s2 = p.REQUESTOR_EMPNAME;
            TempData["email-act2"] = s2;
            //SendEmail(s, ss);
            SendEmail(s);
            db.SaveChanges();
            return View("~/Views/Home/Index.cshtml");
        }
    }
    //return View();
    return View("~/Views/Home/Index.cshtml");
}