【问题标题】:How can i generate name attributes using asp-for that ignore the parent?如何使用忽略父级的 asp-for 生成名称属性?
【发布时间】:2020-09-02 13:20:38
【问题描述】:

例如,我的视图中有这个:

    <div class="form-group">
       <label asp-for="Departamento.Departamento" class="control-label"></label>
       <input name="Departamento" class="form-control" />
       <span asp-validation-for="Departamento.Departamento" class="text-danger"></span>
    </div>

正如您在输入中看到的那样,我有 name="Departamento",因为在我的模型中我有字符串 Departamento.. 我想使用 asp-for="Departamento",但由于我的模型是两个模型的组合,我无法直接访问它,它将是 asp-for="Departamentos.Departamento"... 所以这给了我错误的文本和不会插入数据库...有什么帮助吗?

编辑:我的模型:


    public class Departamentos
        {
            public int Id { get; set; }
            public string Departamento { get; set; }
            public string Autor { get; set; }
            public int Ordem { get; set; }
            public DateTime Criado { get; set; }
            public List<AreasDepartamentos> Areas { get; set; }
    
    
        }
    ```

public class AreasDepartamentos
    {
        public int AreasId { get; set; }
        public Areas Area { get; set; }

        public int DepartamentoId { get; set; }
        public Departamentos Departamento { get; set; }


    }

My controller post :
´´´

    [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Create([Bind("Id,Departamento,Autor,Ordem")] Departamentos departamentos, int[] areas)
            {
                List<AreasDepartamentos> areasLista = new List<AreasDepartamentos>();
               
                foreach(var i in areas)
                {
                    areasLista.Add(new AreasDepartamentos() { AreasId = i, DepartamentoId = departamentos.Id });
                }
    
                departamentos.Areas = areasLista;
                departamentos.Criado = DateTime.Now;
    
                if (ModelState.IsValid)
                {
                    _context.Add(departamentos);
                    await _context.SaveChangesAsync();
                    return RedirectToAction(nameof(Index));
                }
            return View(departamentos);
            }

´´´

【问题讨论】:

  • 它可以很好地与我的项目中的标签助手一起工作。你能分享你的模型吗?你用什么,mvc 或剃刀页面?另外,请分享你想在后端接收的模型是什么.
  • 我会用有问题的模型编辑问题,他们可能有点令人困惑,因为我是新手,但你去吧:)

标签: c# html .net asp.net-core model


【解决方案1】:

在您的情况下,您似乎只能收到 Departamento 的名称。asp-for="Departamentos.Departamento" 会生成 html:name="Departamentos.Departamento"。要仅传递 Departmento,您可以使用忽略后端中的父名称[Bind(Prefix="Departamentos")]:

[HttpPost]
public IActionResult Test([Bind(Prefix = "Departamentos")] YourModel model)
{
    return View();
}

注意:请确定 asp-for 中的数据是什么。您的代码使用 asp-for="Departamento.Departamento",但您的描述是 asp-for="Departamentos.Departamento"。

【讨论】:

  • 是的,它是 asp-for="Departamento.Departamento"。这是我的帖子操作:``` [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind("Id,Departamento,Autor,Ordem")] Departamentos department, int[] area) 我明白为什么要添加前缀,但是我不能在前面有 Bind 中的字段,它仍然可以工作吗?谢谢您的帮助! :)
  • 我试图删除 Bind 中的字段并放置前缀,但它没有工作...如果我在视图中使用 asp-for,它仍然会给我一个错误:/
  • 绑定前缀是忽略后端而不是前端的父名称。标签助手仍然应该是asp-for="Departamento.Departamento"。
  • 这太完美了!非常感谢你! :D
猜你喜欢
  • 2019-02-22
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 2014-05-16
  • 2015-10-26
  • 2022-08-03
  • 1970-01-01
  • 2017-11-12
相关资源
最近更新 更多