0

How can I pass the input type text value to TempData

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

EDIT:

This's the action that I need to send from the View to Controller (Not Controller to view)

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");
    }
BlackSD
  • 101
  • 8

2 Answers2

0

Unless, you want something dynamic that updates TempData["APP"] when the input changes, what you can do is to assign the value when that form is posted. So, you do something of this nature.

public async Task<IActionResult> OnPostAsync(Model model) 
{ 
    model.YourField = TempData["APP"] 
} 
Qudus
  • 1,440
  • 2
  • 13
  • 22
  • Thanks but i need to send the TempData (View) to controller, just I need to copy '' Value to the TempData – BlackSD Feb 17 '21 at 14:15
0

However your question seems to look more of a Post action. In which case you should consider reading up on how to post data using a form. Use this.

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

And set up the controller action like so:

[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");
}
Freek W.
  • 406
  • 5
  • 20
  • Thanks but i need to send the TempData (View) to controller, just I need to copy '' Value to the TempData – BlackSD Feb 17 '21 at 14:15
  • May I ask why you would want to send the element to the controller? It would seem to me your problem would be easily solved by just sending the value of your input to your controller using a form and a submit. – Freek W. Feb 17 '21 at 14:19
  • Because in Textbox I write a comment so i just wanna pass the value that I introduce from the TextBox into the TempData, when I press the button from my form this call a function (I'll Edit the question and add the code) So that function save the data. – BlackSD Feb 17 '21 at 14:35
  • The question is Edit – BlackSD Feb 17 '21 at 14:45
  • The usage of your variables is really unclear. Variable names should be descriptive https://www.informit.com/articles/article.aspx?p=24472 I updated my answer. – Freek W. Feb 17 '21 at 14:57