【问题标题】:The view doesn't render when return View("viewname", model)返回 View("viewname", model) 时视图不呈现
【发布时间】:2021-09-13 16:17:02
【问题描述】:

我正在开发 asp.net core mvc web 应用程序。在我的控制器中,有两个名为“Index”和“Index2”的操作。

当我向“Index2”提交表单时,它将返回View("Index", model)。但是视图没有正确渲染。

例如,如果我在 TextBox 中输入“Steven”并提交到“Index2”操作,则 Name 属性应为“Name999”。 HTML 上的文本框应该显示“Name999”,但实际上,它仍然显示“Steven”。

代码示例:

@model WebApplication2.Controllers.Test

<form method="post" action="/home/Index2">

<div class="form-group">
    
    <input type="text" asp-for="Name"/>
</div>

<div class="form-group">
    <button type="submit">Add</button>
</div>
</form>

public IActionResult Index(Test  test)
{
    return View(test);
}

[HttpPost]
public IActionResult Index2(Test test)
{
    test.Name = "Name999";
    return View("Index",test);
}

public class Test
{ 
    public string Name { get; set; }
}

【问题讨论】:

  • 请解释一下但是视图渲染不正确,请。
  • 我添加了更多信息。
  • 看这个答案,stackoverflow.com/questions/26062359/… 简而言之,The editors like TextBoxFor, DropDownListFor, etc. use the ModelState values instead of the values from the model you passed to the view.
  • 您可以尝试在 CSHTML 视图中将模型成员声明为隐藏变量(就在 @model 声明之后)。像这样:@Html.HiddenFor(x => x.Name);这应该通过视图绑定器绑定模型的会话变量。

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


【解决方案1】:

您将表单提交到索引,而不是 index2

试试这个

查看索引

@model WebApplication2.Controllers.Test

<form asp-action="Index2" method="post">


<div class="form-group">
    <input type="text" asp-for="Name"/>
</div>

<div class="form-group">
    <button type="submit">Add</button>
</div>


</form>

并尝试刷新模型状态,因为您正在回发

ModelState.Clear();
test.Name = "Name999";

或更改视图控件

 <input type="text" asp-for="Name" value="@Model.Name"/>

但是当它是我个人通常使用的同一个控制器时

return Index(test);

【讨论】:

  • 实际上,我将表单提交给“Index2”操作。例如,如果我在 TextBox 中输入 'Steven' 并提交到 'Index2' 操作,Name 属性应该是 'Name999'。 HTML 上的文本框应该显示“Name999”,但实际上,它仍然显示“Steven”。
  • @XieSteven 是的,应该是“Name999”。这意味着您仍然提交到索引。但根据我的代码,它应该是 asp-action="Index2" asp-controller="Home"。请修复代码并重试
  • @XieSteven 在索引视图中也设置了一个断点。看看发生了什么 - 是测试没有改变还是视图没有刷新。
  • @XieSteven 我更新了我的答案。请再试一次。
【解决方案2】:

我可以重现同样的问题,我发现它似乎在 Model 之前从 ModelState 中获得了价值,这导致了上述问题。

要修复它,我们可以尝试清除它,如下所示。

public IActionResult Index( )
    {
      
        return View();
    }

    [HttpPost]
    public IActionResult Index2(Test test)
    {
        ModelState.Clear();
        test.Name = "Name999";
        return View("Index", test);
    }

@model WebApplication99.Models.Test

<form method="post" action="/test/Index2">

    <div class="form-group">

        <input type="text" asp-for="Name" />
    </div>
                            
    <div class="form-group">
        <button type="submit">Add</button>
    </div>
</form>

注意:ModelState 的值

结果:

【讨论】:

  • 1) 定义public IActionResult Index(Test test) { return View(test); } - 此代码存在于问题中,您将看到它。我已经回答了,但是@Xie Steven 没有看我的回答。 2)注意:Index.cshtml已经定义了视图模型@model WebApplication2.Controllers.Test。因此需要使用Test 模型调用View()
猜你喜欢
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多