简体   繁体   中英

Error getting information from two tables for editing - one-to-one relationship. C#

Friends, I'm a beginner and I'm having a hard time getting information from the database of two tables with a one-to-one relationship

I'm currently getting this error:

An unhandled exception occurred while processing the request.


Error

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'LojaVirtual.Models.person', but this ViewDataDictionary instance requires a model item of type 'LojaVirtual.Models.ViewModels.Student.PersonAddressViewModel'.


I tried to replace it with PersonAddressViewModel, but I get an error stating that it is not a database table. I created it only as an intermediate class. How can I get the data?

I have the "person" and "addresses" table.

below I have the two models for "person" and "address".

person.cs

namespace LojaVirtual.Models
{
    public class person
    {
        public int id_person { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public string confirmpassword { get; set; }

        [ForeignKey("id_person")]
        [JsonIgnore]
        public address Address { get; set; }
    }
}

address.cs

namespace LojaVirtual.Models
{
    public class address
    {
        [Key]
        public int id_address { get; set; }
        public string city { get; set; }
        public string state { get; set; }

        [ForeignKey("person")]
        public int? id_person { get; set; }
        public virtual person Person { get; set; }
    }
}

I created an intermediate class. PersonAddress.cs



namespace LojaVirtual.Models.ViewModels.Student
{
    public class PersonAddressViewModel
    {
        [Key]
        public int id_person { get; set; }         
        public string name{ get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public string confirmpassword { get; set; }

        public string city { get; set; }
        public string state{ get; set; }

    }
}

The registration is done normally in the two tables, but I have difficulty in bringing the data filled in with the values of the two tables for editing.

This is my editing method:

        [HttpGet]
        public IActionResult Update()
        {

            person person = _clientRepository.getperson(_loginPerson.GetClient().id_person);
            return View(person);
        }

My View Update.cshtml

@model LojaVirtual.Models.ViewModels.Student.PersonAddressViewModel

@{
    ViewData["Title"] = "Update";
}

<h2>Atualizar</h2>

<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Atualizar">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="id_person" />
            <div class="form-group">
                <label asp-for="name" class="control-label"></label>
                <input asp-for="name" class="form-control" />
                <span asp-validation-for="name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="email" class="control-label"></label>
                <input asp-for="email" class="form-control" />
                <span asp-validation-for="email" class="text-danger"></span>
.
.
.
.

            <div class="form-group">
                <label asp-for="city" class="control-label"></label>
                <input asp-for="city" class="form-control" />
                <span asp-validation-for="city" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="state" class="control-label"></label>
                <input asp-for="state" class="form-control" />
                <span asp-validation-for="state" class="text-danger"></span>
            </div>

EDIT

        public person getperson(int id_person)
        {
            return _db.person.Find(id_person);
        }

I appreciate if anyone can help. Any comment is very welcome!

The Update view expect a PersonAddressViewModel type model, so the Update action needs to return a instance of PersonAddressViewModel to it. As @insane_developer metioned, you can use some mapping library, such as Automapper. It's okay if you haven't heard that, you can simply map it one by one.

[HttpGet]
public IActionResult Update()
{

    person person = _clientRepository.getperson(_loginPerson.GetClient().id_person);
    var personAddressViewModel = new PersonAddressViewModel()
    {
        id_person = person.id_person;
        name= person.name;
        email = person.email;
        password = person.password;
        confirmpassword = person.confirmpassword;
        city = (person.Address == null) ? null : person.Address.city;
        state= (person.Address == null) ? null : person.Address.state
    };
    
    return View(personAddressViewModel);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM