MVC Thumbnail Viewer

My .cshtml index

@model ReportGenerator.WebUI.Models.ReportViewModel @Html.TextBoxFor(m => m.report.FileName, new { @class = "form-control", id = "FileName" }) 

My controller

 public ActionResult Index(ReportViewModel model) { ...some stuff model.report = new Report(); model.report.FileName = "INDEX"; return View(model); } public ActionResult fillFields(ReportViewModel _model) { ...some stuff _model.report = new Report(); _model.report.FileName = "FILL"; return View("Index", _model); } 

When I launch the application, the TextBox Text property is set to "INDEX". Also, when I press a button that calls the action of the fillFields controller, the TextBox still displays "INDEX", it does not change to "FILL".

What am I doing wrong? Why doesn't he want to work?

+7
c # asp.net-mvc asp.net-mvc-4
source share
1 answer

@StephenMuecke answered this correctly in the comments above.

You are not doing anything wrong. Your fillFields() method has a ReportViewModel parameter, so its values ​​are added to ModelState when the method is initialized. When you return the view, your TextBoxFor() method uses the value from ModelState (and not the model property) to set the value of the text field. The reason for this is explained here. The right approach - follow the PRG pattern -

+1
source share

All Articles