Model attributes not passed to next view

I am trying to reuse a model in different views in asp.net mvc. But if the model is passed to the next view and new attributes are submited the old values are lost.

Here the code example I’am using:

Model

    public class ExampleModel
    {
        public string Attribut1 { get; set; }
        public string Attribut2 { get; set; }
    }

Controller
    public class HomeController : Controller
    {
        public IActionResult Page1()
        {
            return View(new ExampleModel());
        }

        public IActionResult Page1Check(ExampleModel model)
        {
            return View("Page2", model);
        }

        public IActionResult Page2Check(ExampleModel model)
        {
            return View("Page2", model);
        }
    }

View1
@model Example.Models.ExampleModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

    <form asp-controller="Home" asp-action="Page1Check">
        <label asp-for="Attribut1"></label>
        <input asp-for="Attribut1" placeholder="Attribut1" />
        <button type="submit">Submit</button>
    </form>

View2
@model Example.Models.ExampleModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

    <form asp-controller="Home" asp-action="Page2Check">
        <label asp-for="Attribut2"></label>
        <input asp-for="Attribut2" placeholder="Attribut2" />
        <button type="submit">Submit</button>
    </form>

If I open Page1 a new model is created. If the attribut1 is entered in Page1 and submited Page1Check is called and the attribut1 is filled. Now the model is passed to page2. Here the attribut2 is filled and submited. But if I check the model inside of Page2Check the attribut1 is null and only attribut2 is filled.

Strangely if I check the attribut1 in the page2 view its still there. Only after the submit the attribut1 is gone.

Do I miss something, how do I prevent that the filled attributs are empty after the submit?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In order for a piece of data to be sent from a user’s browser to the server when the corresponding form is submitted, it must be contained within an HTML element that will be sent to the server (e.g. an <input> or a <select>).

In your first view, you have an input for Attribut1 but not for Attribut2, so Attribut2 won’t be part of the data sent to the server when the form in view 1 is submitted. The same is true for Attribut1 in view 2.

To solve this, you can add hidden fields to each view, contained by the <form> tag, to store the data:

  • For view 1, add <input type="hidden" asp-for="Attribut2" />.
  • For view 2, add <input type="hidden" asp-for="Attribut1" />.

As an example, view 1’s form would now look like this:

<form asp-controller="Home" asp-action="Page1Check">
    <label asp-for="Attribut1"></label>
    <input asp-for="Attribut1" placeholder="Attribut1" />
    <input type="hidden" asp-for="Attribut2" />
    <button type="submit">Submit</button>
</form>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x