如何在不丢失数据的情况下将模型从一个视图传递到另一个视图?

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

我知道这个问题可能很奇怪,但我正在尝试传递填充在一个视图中并返回到我的控制器、另一个视图的数据,以填充所有变量。我的问题是,每次当我将从第一个视图正确接收到的数据传递给第二个视图时,第二个视图 returns 我一个新模型,只有数据,填充在第二视图。为了更好地理解我的代码:

控制器:

    [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");
}

我第一次查看的代码:

    @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>

这里是我的第二个视图的代码:

@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>

这是值的一个实例:

Values received from the first view

将模型传递给第二个视图并接收我的模型数据后更改为:

Values from the second view#1

Values from the second view#2

为什么会发生这种情况以及如何解决这个问题?

我不确定你的问题是否有道理,但这可能只是我对 MVC 缺乏了解。

page lifecycle 指示要构建页面,处理回发,将整个内容发送给用户并尽快从内存中删除。页面(View+Model)在发送给用户后甚至不会在内存中。数据无法保留在模型或视图中。

如果您需要在回发之间保留数据,则必须将其保存在页面外的某个位置。数据库很常见。 ASP.Net 还有一些不太持久的会话相关存储方法。一旦您拥有数据持久性,为模型 A 或模型 B 检索它就是一个微不足道的细节。

return浏览后,是一个新的页面。向视图传递参数是保留数据的一种方式,从数据库重新加载数据是另一种方式。

新页面:数据 -> 模型 -> 视图 -> 操作 -> 消失了!

另一个新页面:数据 -> 模型 -> 视图 -> 操作 -> 消失了!

所以,你必须在操作后从头重新加载。