I am new to ASP.NET Core MVC. I want to move the text string from controllers to views. Here is my controller.
public class CalculatorController : Controller
{
public string text= "";
public IActionResult Index()
{
ViewData["Text"] = text;
return View();
}
public IActionResult Button1_Click(string button)
{
text += button;
return RedirectToAction("Index");
}
public IActionResult Button2_Click(string button)
{
text = text + button;
return RedirectToAction("Index");
}
}
In the controller, I make a public string text, in which the button value be stored when button1_Click, it stores its value and shows in the input textbox and when Button2_Click it stores and show both the values of button1 and button2 in the input textbox.
The input textbox markup is here:
<input type="text" asp-controller="Calculator" asp-action="Index"
value="@ViewBag.Text" class="form-control" />
But this code does not work well.
return xxxlines, and you'll find theViewData["Text"]doesn't keep the value from each other, so you can only set the complete value.