在ASP.NET Core MVC中选择ENUM Tag Helper [英] Select ENUM Tag Helper in ASP.NET Core MVC

查看:157
本文介绍了在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆