Home > database >  Access property of one class to another
Access property of one class to another

Time:10-08

I am trying to save data in list when form is submitted. But in my model one property Phonenumber is coming from other model class and because of that error is coming. Please check below whole code.

Here is my view

@using (Html.BeginForm("Index", "Person"))
{
    <div>
        User : <input type="text" name="Name"/>
    </div>
    <div>
        Contact : <input type="text" name="Contact.PhoneNumber"/>
    </div>
    <div>
        <input type="submit" value="save"/>
    </div>
}

Here is Model class :

namespace WebApplication1.Models
{
    public class Person
    {
        public string Name { get; set; }
        public Contact Contact { get; set; }
    }
}

Here is controller post method when form is submitted:

[HttpPost]
        public IActionResult Index(Person person)
        {
public static List<Person> Persons = new List<Person>();

            var name = person.Name;
            string phone = person.Contact.PhoneNumber;

            Persons.Add(new Person
            {
                Name = name,
                Contact = phone // here error is coming like cannot implicitly convert type 'string' to 'WebApp1.Models.Contact'
            });

            return View("List");
        }

I want to add this data in Persons list when form is submitted..but I am getting error in this line.

Contact = phone // cannot implicitly convert type 'string' to 'WebApp1.Models.Contact'

Any idea here.

Thanks

CodePudding user response:

Contact = phone

You're trying to assign something that doesn't fit. The error already explains it, but let's see how you could've figured this out:

public class Person
{
    public string Name { get; set; }
    public Contact Contact { get; set; }
}

Contact is of type Contact, which I presume is a class in your codebase.

string phone = person.Contact.PhoneNumber;

phone is a string, as you've explicitly confirmed here.

Now, the error message should make sense:

cannot implicitly convert type 'string' to 'WebApp1.Models.Contact'

In other words:

"You can't put a string in a property of type Contact". Doesn't make sense. I refuse to compile this.

Note: the reason why it talks about implicit conversions is because there are ways to make one type assignable to a variable/property of another type. When you assign one type to a variable/property of another type, the compiler will try to see if an implicit cast is possible, but in your case it is not. Also, implicit casting is not the solution you need in this scenario.


I suspect you were trying to do this:

Persons.Add(new Person
{
    Name = name,
    Contact = new Contact()
    {
        PhoneNumber = phone
    }
});

Given the code, you already had a person, so I'm uncertain why you're trying to recreate a new Person instance with the same values. You could've just done:

Persons.Add(person);

and then skipped the var name and string phone declarations entirely.

  • Related