Use Jquery Autocomplete in Id properties

1

I have my Model as follows

public class Album {
   public int Id {get;set;}
   public int ArtistaId {get;set;}
}
public class Artista {
   public int Id {get;set;}
}

And in my View, I use:

  @Html.TextBoxFor(model => model.ArtistaId, new { @class = "form-control" })

Problem:

I wanted to use AutoComplete , so it would fetch and set ArtistaId . However, since it is a " Id ", and when filling that TextBox , it informs ValidationMessage that the field only accepts numeric data

    
asked by anonymous 31.12.2014 / 02:48

1 answer

1

Autocomplete is correct. The behavior would be right if you were filling Id in hand, which does not make sense within this context.

Put in your Model an unmapped text field in the database as follows:

public class Album 
{
    [Key]
    public int Id {get;set;}
    public int ArtistaId {get;set;}

    [NotMapped]
    public String NomeArtista { get; set; }

    public virtual Artista Artista { get; set; }
}

View will look like this:

@Html.HiddenFor(model => model.ArtistaId)
@Html.TextBoxFor(model => model.NomeArtista, new { @class = "form-control" })

Place the Autocomplete in NomeArtista , putting in the success event the fill of ArtistaId .

    
31.12.2014 / 02:56