0

I'm developing asp.net core mvc web application. In my controller, there're two actions named 'Index' and 'Index2'.

When I submit form to 'Index2', it will return View("Index", model). But the view doesn't render correctly.

For example, if I input 'Steven' in the TextBox and submit to 'Index2' action, the Name property should be 'Name999'. The Textbox on the HTML should be show 'Name999', but actually, it still show 'Steven'.

enter image description here

enter image description here

enter image description here

enter image description here

The code sample:

@model WebApplication2.Controllers.Test

<form method="post" action="/home/Index2">

<div class="form-group">
    
    <input type="text" asp-for="Name"/>
</div>

<div class="form-group">
    <button type="submit">Add</button>
</div>
</form>

public IActionResult Index(Test  test)
{
    return View(test);
}

[HttpPost]
public IActionResult Index2(Test test)
{
    test.Name = "Name999";
    return View("Index",test);
}

public class Test
{ 
    public string Name { get; set; }
}
Xie Steven
  • 8,544
  • 1
  • 9
  • 23
  • 1
    Explain **_But the view doesn't render correctly_**, please. – Jackdaw Sep 11 '21 at 15:21
  • I added more information. – Xie Steven Sep 12 '21 at 10:56
  • See this answer, https://stackoverflow.com/questions/26062359/mvc-4-textbox-not-updating-on-postback in nutshell, `The editors like TextBoxFor, DropDownListFor, etc. use the ModelState values instead of the values from the model you passed to the view.` – Rippo Sep 12 '21 at 11:02
  • Can you try declaring the model member as a hidden variable in the CSHTML view (just after the @model declaration). like this: @Html.HiddenFor(x => x.Name); This should bind the session variable for the model through the view binder. – Andrew Halil Sep 12 '21 at 11:10

2 Answers2

0

you are submitting your form to index, not to index2

try this

view Index

@model WebApplication2.Controllers.Test

<form asp-action="Index2" method="post">


<div class="form-group">
    <input type="text" asp-for="Name"/>
</div>

<div class="form-group">
    <button type="submit">Add</button>
</div>


</form>

and try to refresh model state since you are doing a post back

ModelState.Clear();
test.Name = "Name999";

or change the view control

 <input type="text" asp-for="Name" value="@Model.Name"/>

But when it is the same controller I am personaly usually using

return Index(test);
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Actually, I submit the form to 'Index2' action. For example, if I input 'Steven' in the TextBox and submit to 'Index2' action, the `Name` property should be 'Name999'. The Textbox on the HTML should be show 'Name999', but actually, it still show 'Steven'. – Xie Steven Sep 12 '21 at 10:48
  • @XieSteven Yes, it should be "Name999". It means that you are still submit to Index. But according my code it should be asp-action="Index2" asp-controller="Home". Pls fix the code and try again – Serge Sep 12 '21 at 11:48
  • @XieSteven And put a break point inside of Index view too. to see what is happening - is test is not changing or view is not refreshing. – Serge Sep 12 '21 at 11:57
  • @XieSteven I updated my answer. Pls try again. – Serge Sep 12 '21 at 12:08
0

I can reproduce same issue, and I find that it seems to get value from ModelState prior to Model, which cause above issue.

To fix it, we can try to clear it, like below.

public IActionResult Index( )
    {
      
        return View();
    }

    [HttpPost]
    public IActionResult Index2(Test test)
    {
        ModelState.Clear();
        test.Name = "Name999";
        return View("Index", test);
    }

@model WebApplication99.Models.Test

<form method="post" action="/test/Index2">

    <div class="form-group">

        <input type="text" asp-for="Name" />
    </div>
                            
    <div class="form-group">
        <button type="submit">Add</button>
    </div>
</form>

Note: the valus of ModelState

enter image description here

Result:

enter image description here

Tupac
  • 2,590
  • 2
  • 6
  • 19
  • 1) Define `public IActionResult Index(Test test) { return View(test); }` - this code exists in the question, and you will see it. I already answered, but @Xie Steven does not look at my answer. 2) Pay attention: the `Index.cshtml` has defined view model `@model WebApplication2.Controllers.Test`. Therefore it's required to call the `View()` with the `Test` model. – Jackdaw Sep 13 '21 at 08:01