在ASP.NET Core MVC中选择ENUM Tag Helper

编程入门 行业动态 更新时间:2024-10-23 23:34:44
本文介绍了在ASP.NET Core MVC中选择ENUM Tag Helper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现很多exemples绑定一个模型到Selectlist,一些使用ENUM,但所有这些都是关于CREATE动作和Im面对EDIT动作的问题。

我的模型

public class ProspectLog { public int Id {get;组; } public int IdProspect {get;组; } public int IdEmpresa {get;组; } public DateTime Criado {get;组; } public string Usuario {get;组; } public string Descricao {get;组; } public ETipoLog TipoLog {get;组; } public enum ETipoLog { [Display(Name =CADASTRO)] Cadastro = 0, [Display(Name =CONTATO )] Contato = 1, [显示(名称= @TROCA ETAPA)] Troca = 2, [显示(名称= @QUALIFICAÇÃO)] Qualifica = 3, [显示(Name = @EDIÇÃO)] Edicao = 4 } }

在我的基于MVC5的旧项目中,我只是在我的视图中使用了这个,这是足够的。

DROPDOWN

< div class =form-group col-sm-6> < label style =font-weight:bolderfor =txtSituacao>Situação< / label> @ Html.EnumDropDownListFor(model => model.Situacao,htmlAttributes:new {@class =form-control}) @ Html.ValidationMessageFor(model => model.Situacao, ,new {@class =text-danger})< / div>

我尝试使用不同的方式,我coudnt设置下拉列表与数据库项选择编辑动作。我试过这样:

< div class =form-group> < label asp-for =TipoLogclass =col-md-2 control-label>< / label> < div class =col-md-10> < select asp-for =TipoLogclass =form-control>< / select> < span asp-validation-for =TipoLogclass =text-danger>< / span> < / div> < / div>

我也尝试过:

< div class =form-group> < label asp-for =TipoLogclass =col-md-2 control-label>< / label> < div class =col-md-10> < select asp-for =TipoLogasp-items =Html.GetEnumSelectList< TipoLog>()>< / select> < span asp-validation-for =TipoLogclass =text-danger>< / span> < / div> < / div>

但是它让我发生编译错误:

我也尝试将模型绑定到控制器上的ViewBag,这样:

控制器:

ViewBag.Log = new SelectList(lista,Id,Nome);

查看:

code>< div class =form-group col-sm-2> < label asp-for =TipoLogoclass =col-md-2 control-label>< / label> < select asp-for =TipoLogoasp-items =ViewBag.Logclass =form-control>< / select> < span asp-validation-for =TipoLogoclass =text-danger>< / span> < / div>

它的部分工作,下拉列表的项目,但不从数据库中选择正确的项目。它显示列表中的第一个被选中。

解决方案

最后我找到了解决方案!在我发布这个问题一个月之后,看到很多有相同问题的帖子,我可以找到解决方案。

解决方案不是很明显,但是这样我没有编译错误。从Ivan得到的答案并不错,但是有必要在视图中实例化模型:

@using CRM 。模型;

所以,我的下拉列表:

< select asp-for =TipoLogasp-items =Html.GetEnumSelectList< ETipoLog>()class =form-control>< / select>

你可以看到,Visual Studio告诉我,它非常专业,画在灰色,但没有,我得到编译错误。我希望我可以帮助别人。

I need some help with an ENUM dropdown using Tag Helper.

I found lots exemples binding a model to Selectlist and some using ENUM but all of them, about CREATE action, and Im facing problems with EDIT action.

MY MODEL

public class ProspectLog { public int Id { get; set; } public int IdProspect { get; set; } public int IdEmpresa { get; set; } public DateTime Criado { get; set; } public string Usuario { get; set; } public string Descricao { get; set; } public ETipoLog TipoLog { get; set; } public enum ETipoLog { [Display(Name = "CADASTRO")] Cadastro = 0, [Display(Name = "CONTATO")] Contato = 1, [Display(Name = @"TROCA ETAPA")] Troca = 2, [Display(Name = @"QUALIFICAÇÃO")] Qualifica = 3, [Display(Name = @"EDIÇÃO")] Edicao = 4 } }

On my old project based on MVC5 I just used this on my View and it was enough.

DROPDOWN

<div class="form-group col-sm-6"> <label style="font-weight: bolder" for="txtSituacao">Situação</label> @Html.EnumDropDownListFor(model => model.Situacao, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Situacao, "", new { @class = "text-danger" }) </div>

I tried with difent ways and I coudnt set the dropdown with database item select on Edit action. I tried this way:

<div class="form-group"> <label asp-for="TipoLog" class="col-md-2 control-label"></label> <div class="col-md-10"> <select asp-for="TipoLog" class="form-control"></select> <span asp-validation-for="TipoLog" class="text-danger"></span> </div> </div>

I also tried like that:

<div class="form-group"> <label asp-for="TipoLog" class="col-md-2 control-label"></label> <div class="col-md-10"> <select asp-for="TipoLog" asp-items="Html.GetEnumSelectList<TipoLog>()"></select> <span asp-validation-for="TipoLog" class="text-danger"></span> </div> </div>

But it ran me to an compilation error:

I also tryed to bind a model a list to a ViewBag on my controller, this way:

CONTROLLER:

ViewBag.Log = new SelectList(lista, "Id", "Nome");

VIEW:

<div class="form-group col-sm-2"> <label asp-for="TipoLogo" class="col-md-2 control-label"></label> <select asp-for="TipoLogo" asp-items="ViewBag.Log" class="form-control"></select> <span asp-validation-for="TipoLogo" class="text-danger"></span> </div>

Its worked partially, the drop down listed the items, but not selecting the correct item from database. it show the first on the list as selected.

解决方案

Finally I found the solution!!! After one month since I had posted this question, and seeing lots of posts with same problem I could found the solution.

The solution not apear to be obvious but, that way i got no compilation erros. The answer I got from Ivan was not incorrect, but it was necessary to instance the model on the view like:

@using CRM.Model;

So, my dropdown:

<select asp-for="TipoLog" asp-items="Html.GetEnumSelectList<ETipoLog>()" class="form-control"></select>

You can see, Visual Studio told me it eas unessessary, painting it in grey, but without that, i get compilation error. I hope I can help someone else.

更多推荐

在ASP.NET Core MVC中选择ENUM Tag Helper

本文发布于:2023-11-17 06:54:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Core   NET   ASP   MVC   Helper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!