I’m new to MVC and I would like to create a simple view that allows a user to enter some data => submit the form => perform some calculation on the server => and post back a model with the calculated data to the same view. I found a way to make it work, but I’m not sure if this is the best way to do it and what bothers me is that after the submit, all the data from the model properties are visible in the browser address bar : http://localhost:53718/?Number=4&Result=16
Here is the code for a very simplified szenario:
The Model:
public class CalculationModel
{
public int Number { get; set; }
public int Result { get; set; }
}
The Controler:
public class HomeController : Controller
{
public ActionResult Index(CalculationModel model)
{
return View(model);
}
[HttpPost]
public ActionResult Calculate(CalculationModel model)
{
model.Result = model.Number * model.Number;
return RedirectToAction("Index", "Home", model);
}
}
The View:
@model WebApplication1.CalculationModel
@{
ViewBag.Title = "Home Page";
}
<h2>Simple Calculation</h2>
@using (Html.BeginForm("Calculate", "Home", FormMethod.Post)){
<div class="form-horizontal" data-crypto-conversion-form>
<div class="form-group">
<div class="input-group">
@Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control " } })
</div>
</div>
<div class="form-group">
<div class="input-group">
@Html.EditorFor(model => model.Result, new { htmlAttributes = new { @class = "form-control " } })
</div>
</div>
<div class="form-group">
<input type="submit" value="Convert" class="btn btn-primary btn-sm submitConversion" />
</div>
</div>
}
I’m not sure if it’s a good idea to do a RedirectToAction from the HttpPost method: return RedirectToAction("Index", "Home", model); - are there any better ways ?
Is there any way to prevent the model property values from displaying in the url / browser address bar ?
Thanks a lot for your help.