【问题标题】:Passing the input value from controller to view in MVC将输入值从控制器传递到 MVC 中的视图
【发布时间】:2022-01-12 05:50:37
【问题描述】:

我是 ASP.NET Core MVC 的新手。我想将文本字符串从控制器移动到视图。这是我的控制器。

public class CalculatorController : Controller
{
    public string text= "";

    public IActionResult Index()
    {
        ViewData["Text"] = text;
        return View();
    }

    public IActionResult Button1_Click(string button)
    {
        text += button;
        return RedirectToAction("Index");
    }

    public IActionResult Button2_Click(string button)
    {
        text = text + button;
        return RedirectToAction("Index");
    }
}

在控制器中,我创建了一个公共字符串文本,其中存储了按钮值button1_Click,它存储它的值并显示在输入文本框中,当Button2_Click 它存储并显示按钮1 的值和输入文本框中的 button2。

输入文本框标记在这里:

<input type="text" asp-controller="Calculator" asp-action="Index"  
       value="@ViewBag.Text" class="form-control" />

但是这段代码效果不好。

【问题讨论】:

  • 我不确定这个 MVC 是做什么的。例如,如果我点击 Button1 两次会显示什么?
  • 你可以在return xxx行设置断点,你会发现ViewData["Text"]之间并没有保持值,所以只能设置完整的值。

标签: .net-core asp.net-core-mvc


【解决方案1】:

您需要考虑到,当您执行操作方法public IActionResult Index() 时,您的控制器类有一个实例。但是在从视图中发布一个新的文本值之后,控制器类的一个新实例将被创建。为了看到这一点,我添加了控制器类哈希码的打印:Debug.WriteLine("[HttpGet] This is: " + this.GetHashCode());

这就是为什么你的代码中的这个操作符text += button; 不能像你预期的那样工作。

为了在请求之间保留文本,在此示例中,使用了TempData

控制器中的代码片段:

public IActionResult Index()
{   
    System.Diagnostics.Debug.WriteLine("[HttpGet] This is: " + this.GetHashCode());
    TempData["TextData"] = "Hello World!";
    return View();
}

[HttpPost]
public IActionResult Index(string data)
{
    System.Diagnostics.Debug.WriteLine("[HttpPost] This is: " + this.GetHashCode());
 
    TempData["TextData"] += data;
    ModelState.Remove("data"); // The context clean up 
    return View("Index");
}

观点Index.cshtml

@{
    var data = TempData["TextData"];   
}

@using (Html.BeginForm("Index", "Calculator"))
{
    <input type="text" asp-for="@data" />
    TempData.Keep("TextData"); // To preserve the data at the end of the request
}

有关更多信息,请参阅以下帖子:Session and state management in ASP.NET Core

【讨论】:

  • 哦,谢谢!!但是当我点击button1时比Button1_Click中的文本字符串值为1,但是在RedirectToAction("Index")之后,比索引动作方法中的文本字符串值为Null。
  • @Shahriyar Rahim:查看更新后的答案。谢谢。
【解决方案2】:

在我看来,如果你只需要将按钮的文本值附加到你的输入框,你甚至不需要向你的后端控制器发出请求,除非你需要做一些其他的操作。如果是这样,我建议您在这里使用 ajax 仅将按钮文本字符串发送到您的控制器,然后您可以在成功回调函数中做一些这样的事情:

public IActionResult Index()
{
    ViewData["Text"] = "asdf";
    return View();
}

public string Button1(string button)
{
    // do your own business here
    return text;
}

public string Button2(string button)
{
    // do your own business here
    return text;
}

而在cshtml文件中,

<input type="text" id="input1" value="@ViewBag.Text " />
<a href="#" id="ccc">ccc</a>
<a href="#" id="ddd">ddd</a>

<script>
    $("#ccc").click(function () {
        $.ajax({
            url: "https://localhost:44372/home/Button1",
            type: "get",
            data: {
                button: "ccc"
            },
            success: function (data) {
                $("#input1").val($("#input1").val() + data);
            }
        });
    });

    $("#ddd").click(function () {
        $.ajax({
            url: "https://localhost:44372/home/Button1",
            type: "get",
            data: {
                button: "ddd"
            },
            success: function (data) {
                $("#input1").val($("#input1").val() + data);
            }
        });
    });
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多