I’m trying to do that:
- Create a Model, add it on a session and send it to the view.
- Change Model fields on my view
- Get the Model from session updated on my controller
The problem is that my model is never updated when I’m changing values on textboxes, I’m sure that I’m missing something with razor,
View:
@model MvcTestApp.Models.Car
<div class="b1">
<div class="b2">@Html.EditorFor(e => e.KM)</div>
<div class="b2">@Html.EditorFor(e => e.RegistrationNumber)</div>
</div>
@Html.ActionLink("Car", "sendCar")
Controller:
On SendCar, I would like to get the model updated.
namespace MvcTestApp.Controllers
{
public class CarController : Controller
{
public ActionResult Show()
{
var model = new MvcTestApp.Models.Car()
{
RegistrationNumber ="12345",
KM = "12345"
};
Session["temp"] = model;
return View("Show",Session["temp"]);
}
public ActionResult sendCar()
{
return View("Show", Session["temp"]);
}
}
}
Model:
namespace MvcTestApp.Models
{
public class Car
{
[DataType(DataType.Text)]
public string KM { get; set;}
[DataType(DataType.Text)]
public string RegistrationNumber { get; set;}
}
}
You need to make your sendCar controller to update the model. Currently, all the changes you do will only persist locally until you navigate away from the page. You need to post the changed model back to the server.
Take a look at the “Handling edits” part of this example to see how it can be done:
Asp.net tutorials