Create Views in ASP.NET MVC 4

2

I am working with Visual Studio 2012, ASP.NET MVC 4, C #, Entity Framework - Code Firts, the IDs of the entities are autonumeric.

The problem is when I create a view, it creates a ProviderId field in the view, but I do not need it since the IDs are autonumeric. I show my model object.

public class ProveedorDto
{
    public int ProveedorId { get; set; }

    [Display(Name = "Razón Social")]
    public string RazonSocial { get; set; }

    [Display(Name = "Dirección")]
    public string Direccion { get; set; }
}

That is the model with which I create the Supplier / Create View

    
asked by Pedro Ávila 05.12.2016 в 00:23
source

3 answers

0

The problem is that EF is not recognizing the value SupplierId as Key, so when creating a strongly typed view it sees it as one more field of the form. Use the exact name of the bone class

public int ProveedorDtoId {get; set;}

or simply call it public int Id {get; set;} that EF maps it as its default Id

The other way would be to set the Annotation [Key] to define the name you would like as the primary key example:

using System.ComponentModel.DataAnnotations;

 public class ProveedorDto
{
    [Key]
    public int ProveedorId { get; set; }

    [Display(Name = "Razón Social")]
    public string RazonSocial { get; set; }

    [Display(Name = "Dirección")]
    public string Direccion { get; set; }
}

This way you are defining which SupplierId is your key. Remember to do the migration after these changes and give a Rebuild to your project so that the view generator does not get sick. Greetings

    
answered by 17.07.2017 / 20:35
source
0

You could try using [NotMapped] in the following way:

public class ProveedorDto
{
 [NotMapped]    
 public int ProveedorId { get; set; }

 [Display(Name = "Razón Social")]
 public string RazonSocial { get; set; }

 [Display(Name = "Dirección")]
 public string Direccion { get; set; }
}

Note: The [NotMapped] attribute is included in the namespace System.ComponentModel.DataAnnotations in EF < 5.

In EF > 5 is in System.ComponentModel.DataAnnotations.Schema

    
answered by 05.12.2016 в 12:30
0

Another variant that you can apply is to work with ViewModels.

A ViewModel is a class whose properties would be exactly what you need to show in the specific view for which it is made, because in theory you can make a ViewModel for each view.

Another important detail is that in the case of the view Create , for example you could use two ViewModels, one that would be the model that would use the view, and the other would be the one that would receive the action method Create with the attribute [HttpPost] , this in case it was convenient, but it can be enough with the same ViewModel for both methods of action.

In this way, your ViewModel for the view Create , let's say you call ProveedorDtoViewModel would have only the two properties you want ( RazonSocial and Direccion ) and when you go to do the view, you select as Clase de modelo to the ViewModel that you created for this view ( ProveedorDtoViewModel ).

Then, to pass the information of your Entity ( ProveedorDto ) to the corresponding ViewModel ( ProveedorDtoViewModel ) you can do it manually or using the tool Automapper .

I know that with this explanation you may not solve much, but at least it is an introduction and I hope it will be useful.

Greetings.

    
answered by 28.02.2017 в 22:57