【问题标题】:@Html.EditorFor targeting wrong property@Html.EditorFor 定位错误的属性
【发布时间】:2020-01-24 13:27:55
【问题描述】:

我在编辑数据的地方拥有剃刀视图。

我的课程是这样的:

public class A
{
    public string Value1 { get; set; }
    public int Value2 { get; set; }

    public A()
    {
        Value1 = "First";
        Value2 = 2;
    }
}

public class B : A
{
    public int Value3 { get; set; }
    public new CustomClass Value1 { get; set; }

    public B()
    {
        Value3 = 3;
        Value2 = new CustomClass();
    }
}

现在当我打电话给我的Razor View 时,我会这样称呼它:

public IActionResult Edit()
{
    return View(new B());
}

在我的cshtml 代码中,我的设置如下:

@using (Html.BeginForm("Apply", "MyController", FormMethod.Post))
{
    @Html.HiddenFor(x => x.Value1);


    <label>Change value 3</label>
    @Html.EditorFor(x => x.Value3);

    <button class="button1">Apply!</button>
}

现在由于某种原因,当我应用更改时,它们确实应用了,但 Value1 参数错误。

我检查了代码的入口,我看到Value1 具有良好的价值,检查了/MyController/Apply 代码,它的值为 null。

当我在cshtml 中设置断点以查看哪个值具有@HiddenFor(x =&gt; x.Value1) 时,我看到了一些奇怪的东西,就是这个(图像中的标记属性在上面的代码中表示为Value2):

如您所见,由于某种原因,razor 具有两种属性,但一种具有null(他在内部视图中)和一种具有良好值的属性。

我能做什么?

【问题讨论】:

    标签: c# asp.net asp.net-core razor razor-pages


    【解决方案1】:

    您的代码存在几个问题。第一个是您不能将新的 CustomClass 分配给 Value2,因为它不会被运算符 new 覆盖(与 Value1 相同)。

    另一个是您必须在 Razor 页面 (cshtml) 的顶部包含您的视图模型@model &lt;PathToViewModel&gt;.B,然后才能开始使用它。

    有工作示例:

    查看模型

    public class A
    {
        public string Value1 { get; set; }
        public int Value2 { get; set; }
    
        public A()
        {
            Value1 = "First";
            Value2 = 2;
        }
    }
    
    public class B : A
    {
        public int Value3 { get; set; }
    
        // This line overriding Value1 from the parent class A, so you can now assign it to a boolean (or CustomClass) value
        public new bool Value1 { get; set; }
    
        public B()
        {
            Value3 = 3;
            // Value2 = new CustomClass(); //Error since Value2 is declared to integer in the parent class
    
            // What you can do is to assign Value2 to a new integer value
            Value2 = 8;
        }
    }
    

    剃刀页面

    @model Web.ViewModels.Account.B
    @using (Html.BeginForm("MyAction", "MyController", FormMethod.Post))
    {
    
        @Html.HiddenFor(x => x.Value1);
    
        <p>Value 1: @Model.Value1</p> // prints: False
        <p>Value 2: @Model.Value2</p> // prints: 8
        <p>Value 2: @Model.Value3</p> // prints: 3
    
        <label>Change value 3</label>
        @Html.EditorFor(x => x.Value3);
    
        <button class="button1">Apply!</button>
    }
    

    控制器中的动作是正确的。

    发布操作:

    我还鼓励您使用标签助手而不是 HTML 助手,如果您使用的是 .NET Core,可以阅读here 了解更多信息。下面是一个使用标签助手编写的类似示例。

    @model Web.ViewModels.Account.B
    <form asp-controller="MyController" asp-action="MyAction" method="post">
    
        <input type="hidden" asp-for="Value1" />
    
        <label asp-for="Value3">Change value 3</label>
        <input asp-for="Value3" />
    
        <button class="button1">Apply!</button>
    
    </form>
    

    【讨论】:

      【解决方案2】:

      我得到了预期的响应。我正在使用 .Net 4.6 MVC。您的 POST 操作是什么样的?

       [HttpPost]
          public ActionResult Apply(B b)
          {
              ViewBag.Message = "Your application description page.";
      
              return View();
          }
      

      【讨论】:

        猜你喜欢
        • 2014-06-20
        • 1970-01-01
        • 2012-04-09
        • 2019-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-24
        相关资源
        最近更新 更多