ASP.NET MVC 无状态性质

发布于 2024-12-20 11:19:51 字数 2052 浏览 1 评论 0原文

这是一个概念性问题。

据我所知,MVC具有无状态的性质。 当我在浏览器中点击重新加载 (F5) 时,我在视图中写入或更改的值不会消失,也不会被模型中的原始值替换。

有谁知道为什么会发生这种情况? 我假设在重新加载页面时,修改后的“未提交”值应该消失。

我的观点只是剃刀代码...我不使用 Web 表单(我的意思是,我的应用程序中没有 .aspx 页面)


@model PruebaMVC.Models.DatosLINQ.OPERACION

@{
    ViewBag.Title = "Edit";
}

<h2>Editar Operación</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Operación</legend>

        @Html.HiddenFor(model => model.IDOperacion)

        <div class="editor-label">
            @Html.LabelFor(model => model.FechaOperacion, "Fecha de la Operación")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FechaOperacion)
            @Html.ValidationMessageFor(model => model.FechaOperacion)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Comision, "Comisión")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comision)
            @Html.ValidationMessageFor(model => model.Comision)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Legajo, "Número de Legajo")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Legajo)
            @Html.ValidationMessageFor(model => model.Legajo)
        </div>

        <p>
            <input type="submit" class="formbutton" value="Guardar Cambios" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Volver al listado de Operaciones", "Index")
</div>

我希望不要通过提出概念性问题来强加网站的任何规则。如果是的话,请告诉我,这样我就不会再这样做了。

this is a conceptual question.

As far as I know, MVC has a stateless nature.
When I hit reload (F5) in my browser, the values I wrote or changed in the view, doesn't go away nor get replaced by the original values from the model.

Does anyone know why is this happening?
I'm assuming that in the moment I reload the page, the modified "not submitted" values should dissapear.

My views are only razor code... I'm not working with web forms (I mean, there are NO .aspx pages in my application)


@model PruebaMVC.Models.DatosLINQ.OPERACION

@{
    ViewBag.Title = "Edit";
}

<h2>Editar Operación</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Operación</legend>

        @Html.HiddenFor(model => model.IDOperacion)

        <div class="editor-label">
            @Html.LabelFor(model => model.FechaOperacion, "Fecha de la Operación")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FechaOperacion)
            @Html.ValidationMessageFor(model => model.FechaOperacion)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Comision, "Comisión")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comision)
            @Html.ValidationMessageFor(model => model.Comision)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Legajo, "Número de Legajo")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Legajo)
            @Html.ValidationMessageFor(model => model.Legajo)
        </div>

        <p>
            <input type="submit" class="formbutton" value="Guardar Cambios" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Volver al listado de Operaciones", "Index")
</div>

I hope not be inflicting any rule of the site by making a conceptual question. If I'm, please tell me, so I don't do this again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆沫 2024-12-27 11:19:51

ASP.NET MVC HTML 帮助程序将使用发布的值来重新创建表单。如果您想确保模型的值被推送到您的视图,请将它们从控制器的模型状态中删除。

您可以使用其中之一

public ActionResult MyController (MyModel model)
{
    if (ModelState.IsValid)
    {
        ModelState.Clear ();
    }

    return View (model);
}

,也可以删除要更新的特定项目,

public ActionResult MyController (MyModel model)
{
    if (ModelState.IsValid)
    {
        ModelState.Remove ("PropertyNameToRemove");
    }

    return View (model);
}

我不确定您在哪里进行更新(在控制器中手动或在模型本身中),但您只需删除任何或全部来自 ModelState 的项目,它们将被重新填充。

另请参阅此问题

ASP.NET MVC HTML helpers will use the values posted to recreate the form. If you want to ensure that your model's values get pushed to your view, remove them from the model state in your controller.

You can use either

public ActionResult MyController (MyModel model)
{
    if (ModelState.IsValid)
    {
        ModelState.Clear ();
    }

    return View (model);
}

or you can remove specific items you want to update

public ActionResult MyController (MyModel model)
{
    if (ModelState.IsValid)
    {
        ModelState.Remove ("PropertyNameToRemove");
    }

    return View (model);
}

I'm not sure where you're doing the updating (either manually in the controller, or in the model itself) but you just have to remove any or all of the items from ModelState and they will be repopulated.

See this question, as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文