0

My index view : have text, subtext, and result

            <div class="control-group">
                @Html.LabelFor(m => m.Result, new { @class = "control-label" })
                <div class="controls">
                    @Html.TextBoxFor(m => m.Result, new { @class = "span6" })
                </div>
            </div>

My Homecontroller have Action Index

[HttpPost]
            public ActionResult Index(HomeViewModel vm)
            {
                var input = vm.Text;
                var subText = vm.SubText;
                IMatchString mString = new MatchStringProvider();
                string result = mString.MatchString(input, subText).ToString();
                vm.Result = String.IsNullOrEmpty(result) ? "no matches" : result;

                return View(vm);
            }

when i post data to action Index and return the view but textbox of result not display the result what did i do wrong, help me fix this problem

beginerdeveloper
  • 785
  • 4
  • 17
  • 43
  • 1
    Because the value of `Result` is added to `ModelState` and the `TextBoxFor()` method uses `ModelState` values to set the value if they exist (refer [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for more detail). You can use `ModelState.Clear();` before updating the value, but the correct approach is to use the PRG pattern and redirect. –  Dec 01 '16 at 07:40
  • 1
    I don't think @StephenMuecke's answer is correct. You can alter the view model data before rendering it. If you put a breakline on the first line of that method do you see the correct values in the "vm" parameter? Also, what are your form data keys/values? – Peter Morris Dec 01 '16 at 07:44
  • 1
    @PeterMorris, Read the link! –  Dec 01 '16 at 07:44
  • i used return RedirectToAction("Index", vm); and this worked – beginerdeveloper Dec 01 '16 at 07:47
  • I stand (sit) corrected, thank you – Peter Morris Dec 01 '16 at 09:12
  • @StephenMuecke Now that I think of it I recall seeing this behaviour. I remember writing some HtmlHelper methods for dropdown lists. I can't believe it slipped my mind that ValueProviders take priority over the ViewModel. Oh well, thanks for correcting me :) – Peter Morris Dec 01 '16 at 20:34

0 Answers0