Click here to Skip to main content
15,038,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get old values after submitting data in asp.net mvc
-----------------------------------------------------------------------
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<input type="text" id="txtNumber1" name="txtNumber1" />
<input type="text" id="txtNumber2" name="txtNumber2" />
<input type="submit" id="btnSubmit" value="Add" />
<input type="text" id="txtNumber3" name="txtNumber3" />
}
------------------------------------------------------------------------
[HttpGet]
[ActionName("Index")]
public ActionResult Index_Get()
{
return View();
}

[HttpPost]
[ActionName("Index")]
public ActionResult Index_Post()
{
string number1 = Request.Form["txtNumber1"];
string number2 = Request.Form["txtNumber2"];
string number3 = (int.Parse(number1) + int.Parse(number2)).ToString();
return View();
}
Posted
Updated 22-May-21 14:45pm

change
HTML
<input type="text" id="txtNumber1" name="txtNumber1" />
<input type="text" id="txtNumber2" name="txtNumber2" />
<input type="submit" id="btnSubmit" value="Add" />
<input type="text" id="txtNumber3" name="txtNumber3" />

to
HTML
@Html.TextBox("txtNumber1")
@Html.TextBox("txtNumber2")
@Html.TextBox("txtNumber3")
@Html.TextBox("txtNumber1")
<input type="submit" id="btnSubmit" value="Add" />
   
Comments
Ranga Maddileti 17-May-14 11:35am
   
hi damithSL ,still i am getting same,after clicking add button i lost all values in textbox's.
i was also facing the same issue so here is the solution

HTML
<input type="text" id="txtNumber1" name="txtNumber1" value="@Request["txtNumber1"]"/>
<input type="text" id="txtNumber2" name="txtNumber2" value="@Request["txtNumber2"]"/>
<input type="submit" id="btnSubmit" value="Add" />
<input type="text" id="txtNumber3" name="txtNumber3" value="@Request["txtNumber3"]"/>


actually when you post form the values get submitted to the server and a new form loads up
so this simple syntax allot the value property simply
value = "@Request["name of the text input"]"


and so on you can do for other text inputs
   
v6
Comments
Member 10929239 9-Apr-20 7:59am
   
Thanks , this is work but how it work combo or check box or radio button ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900