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