简体   繁体   English

在 controller 和多个视图之间传递 model

[英]Passing model between controller and multiple views

I'm new to asp.net and am having a pretty basic issue (also don't have much experience in c# so I probably am misusing terminology).我是 asp.net 的新手,并且遇到了一个非常基本的问题(在 c# 方面也没有太多经验,所以我可能误用了术语)。 I have 2 views which are connected to the same model.我有 2 个视图连接到同一个 model。 In the first view the user input assigns a value to one of the model attributes - lets call it A1.在第一个视图中,用户输入为 model 属性之一分配了一个值——我们称之为 A1。 In the second view, which relies on A1's value, the user input fills the rest of the model attributes, as seen in the example below.在依赖于 A1 值的第二个视图中,用户输入填充 model 属性的 rest,如下例所示。

Controller looks something like this: Controller 看起来像这样:

    public ActionResult ClalledFromView1(MyModel model)
    {
        return View("View2", model);
    }

    public ActionResult CalledFromView2(MyModel model)
    {
        model.CheckAllAttributes();
        return View("View3");
    }

View1 looks something like this: View1 看起来像这样:

 <html> @using (Html.BeginForm("CalledFromView1", "ControllerName", FormMethod.Post)) { @Html.TextBoxFor(model => model.A1) <button type="submit">Submit</button> } </html>

View 2 looks something like this:视图 2 看起来像这样:

 <html> @using (Html.BeginForm("CalledFromView2", "ControllerName", FormMethod.Post)) { @if (Model.A1 == "some value") { @Html.TextBoxFor(model => model.A2) } else { @Html.TextBoxFor(model => model.A3) } <button type="submit">Submit</button> } </html>

What's happening is that by the time CalledFromView2 is called, A1 is empty but A2 is filled and is consistent with A1's original value.发生的情况是,调用 CalledFromView2 时,A1 为空,但 A2 已填充,并且与 A1 的原始值一致。 I want the same model being passed back and forth between controller and both views without clearing any attributes.我希望在 controller 和两个视图之间来回传递相同的 model 而不清除任何属性。 TIA. TIA。

If you want to keep the A1 value passed,you can add a hidden input like this.如果你想保持 A1 值通过,你可以像这样添加一个隐藏的输入。

View2:视图2:

 @using (Html.BeginForm("CalledFromView2", "ControllerName", FormMethod.Post))
    {
        @Html.TextBoxFor(model => model.A1, new { @hidden = "hidden" })
        @if (Model.A1 == "some value")
        {
            @Html.TextBoxFor(model => model.A2)
        }
        else
        {
            @Html.TextBoxFor(model => model.A3)
        }
        <button type="submit">Submit</button>
    }

Here is a demo:这是一个演示:

Model: Model:

public class ModelA
    {
        public string A1 { get; set; }
        public string A2 { get; set; }
        public string A3 { get; set; }


    }

Controller(Test3):控制器(测试3):

public IActionResult TestModelA()
        {
            return View();
        }
        public ActionResult CalledFromView1(ModelA model)
        {
            return View("View2", model);
        }

        public ActionResult CalledFromView2(ModelA model)
        {
            //model.CheckAllAttributes();
            return View("View3");
        }

TestModelA:测试模型A:

@using (Html.BeginForm("CalledFromView1", "Test3", FormMethod.Post))
{
    @Html.TextBoxFor(model => model.A1)
    <button type="submit">Submit</button>
}

View2:视图2:

@using (Html.BeginForm("CalledFromView2", "Test3", FormMethod.Post))
{
    @Html.TextBoxFor(model => model.A1, new { @hidden = "hidden" })
    @if (Model.A1 == "some value")
    {
        @Html.TextBoxFor(model => model.A2)
    }
    else
    {
        @Html.TextBoxFor(model => model.A3)
    }
    <button type="submit">Submit</button>


}

result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM