Home > database >  I couldn't figure out the MVC Logic
I couldn't figure out the MVC Logic

Time:12-22

Entity is:

public partial class Persons
{
public int Id { get; set; }

public string? Name { get; set; }

public string? Surname { get; set; }

public string? Number { get; set; }

public string? Address { get; set; }

public string? PublicId { get; set; }

public string? FatherName { get; set; }

public string? MotherName { get; set; }

}

Also Model is:

public partial class OnlyPrivateProp
{
public int Id { get; set; }

public string? Name { get; set; }

public string? Surname { get; set; }
}

And controller:

 public class DemoController : Controller
 {
    _context c;

    public DemoController(_context c)
    {
        this.c = c;
    }

    public IActionResult Index()
    {
        ET model= new ET();
        var select = ..............



        return View();
    }
}

I dont want to use all data to display only three colunms. So I defined a model has three prop. But I cant pass data to model and cant select data and cant send controller.

CodePudding user response:

You could create different ViewModel for different Views

The entities models are mapped to the tables in your database,you could check the documents related with EFCore

For your requirement,you could fill the viewmodel and pass the value to your View as below:

[Table("SomeTable")]
    public class SomeEntity
    {
        public int Id { get; set; }
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
        public string Prop3 { get; set; }
        ...........

    }

    public class ViewModel
    {
        
        public string Prop1 { get; set; }
        
        public string Prop3 { get; set; }

    }

public IActionResult SomeAction()
        {

            ...........
            var vmlist = _context.SomeEntity.Select(x => new ViewModel{ Prop1 = x.Prop1, Prop3 = x.Prop3 }).ToList();

            return View(vmlist);
        }

And the documents related with MVC

  • Related