Search code examples
c#asp.netvisual-studioasp.net-mvc-4asp.net-mvc-viewmodel

How do I pass a Model from a View, to another view, without data getting Lost?


I know the question might be strange, but i'm trying to pass data, that is filled in a View and returned to my Controller, to another View, to fill all variables. My Problem is, that every time when I pass the data, that is correctly received from the first View, to the second View, the second View returns me a new Model, with only the data, that is filled in the second View. For better understanding here my code:

Controller:

    [HttpPost]
public IActionResult SubmitCharInfo(Character character) {
    //charRepo.SaveCharacter(character);
    return View("Char_StandartAttributes", character);
}

[HttpPost]
public IActionResult CharAllSkills(Character character) {
    charRepo.UpdateCharacter(character);
    return View("Char_FirstSkillPage");
}

The Code from my first View:

    @model Character

<form method="post" asp-action="SubmitCharInfo">
    <input type="hidden" asp-for="ownerID" />
    <input type="hidden" asp-for="charID" />
    <table  class="table table-bordered table-dark text-center">
        <tbody>
            <tr>
                <th>Enter a character name:</th>
                <td><input asp-for="charName" class="input-validation-error" data-for="true" data-val-required="Please enter your phone number" /></td>

            </tr>
            <tr>
                <th>Enter a age:</th>
                <td><input asp-for="charAge"/></td>
            </tr>
            <tr>
                <th>Enter a job:</th>
                <td><input asp-for="charJob" /></td>
            </tr>
            <tr>
                <th>Enter a place of residence:</th>
                <td><input asp-for="charPOR" /></td>
            </tr>
            <tr>
                <th>Enter a gender:</th>
                <td><input asp-for="charGender" /></td>
            </tr>
            <tr>
                <th>
                    <div data-valmsg-for="charStory" data-valmsg-replace="true" class="alert-danger"></div>
                    Enter a story:
                </th>
                <td><input asp-for="charStory" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <button type="submit" class="btn">Submit</button>
                </td>
            </tr>
        </tbody>
    </table>
</form>

And here the Code of my second View:

@model Character


<form method="post" asp-action="CharAllSkills">
    <table class="table table-bordered table-dark text-center">
        <thead>
            <tr>
                <th colspan="2"><h1>Standart Skills</h1></th>
            </tr>
            <tr>
                <th colspan="2"><h3>Enter a value for...</h3></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <th>close range combat:</th>
                <td><input asp-for="standartSkills.AttackeNahkampf" /></td>
            </tr>
            <tr>
                <th>stamina:</th>
                <td><input asp-for="standartSkills.Ausdauer" /></td>
            </tr>
            <tr>
                <th>inteligence:</th>
                <td><input asp-for="standartSkills.Intelligenz" /></td>
            </tr>
            <tr>
                <th>strength: </th>
                <td><input asp-for="standartSkills.Koerperkraft" /></td>
            </tr>
            <tr>
                <th>strength:</th>
                <td><input asp-for="standartSkills.MentaleBelastbarkeit" /></td>
            </tr>
            <tr>
                <th>weapon handling:</th>
                <td><input asp-for="standartSkills.Waffenumgang" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <button type="submit" class="btn">Submit</button>
                </td>
            </tr>
        </tbody>
    </table>
</form>

Here is a live example of values:

Values received from the first view

After passing the Model to the second view and receiving my model data changed to:

Values from the second view#1

Values from the second view#2

Why does this happen and how to fix this?


Solution

  • After return view, it is a new page. Passing parameters to the view is one way to retain data or reloading data from the database is another way.

    New page: Data -> Model -> View -> Action -> gone!

    Another new page: Data -> Model -> View -> Action -> gone!

    So, you have to reload from the beginning after action.