Search code examples
c#asp.netphone-number

Phone number in asp.net controller


I'm making a request system that'll be used for finding data related to a customer but I'm having a small problem, how on earth do you write the code for a phone number in the controller of ASP.NET? This is what I have so far in the controller:

 public ActionResult Person()
    {
        Person hooman = new Person()
        {
            FirstName = "Parker",
            LastName = "Smith",
            Age = 30,
            DateOfBirth = new DateTime(1988, 01, 01),
            Email = "psmith@gmail.com",
            //Telephone = 0851943376,
            Smoker = "No",
            SeriousIllness = "No",
        };

        return View(hooman);
    }

And just in case it's needed, here's a bit of the very rudimentary view for the Person controller:

<table>
<tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Age</th>
    <th>Date Of Birth</th>
    <th>Email</th>
    <th>Telephone</th>
    <th>Smoker</th>
    <th>Serious Illness</th>
</tr>
<tr>
    <td> @Html.DisplayFor(model => model.FirstName)</td>
    <td> @Html.DisplayFor(model => model.LastName) </td>
    <td> @Html.DisplayFor(model => model.Age) </td>
    <td> @Html.DisplayFor(model => model.DateOfBirth) </td>
    <td> @Html.DisplayFor(model => model.Email)</td>
    @*<td> @Html.DisplayFor(model => model.Telephone)</td>*@
    <td> @Html.DisplayFor(model => model.Smoker)</td>
    <td> @Html.DisplayFor(model => model.SeriousIllness)</td>
</tr>

I can't for the life of me figure out how to get it to work. Everything else is fine, it's just the Telephone part I can't figure out. Any help at all would be greatly appreciated!


Solution

  • Looks like the problem is you are trying to treat the telephone number as an int and not as a string.

    public ActionResult Person()
        {
            Person hooman = new Person()
            {
                FirstName = "Parker",
                LastName = "Smith",
                Age = 30,
                DateOfBirth = new DateTime(1988, 01, 01),
                Email = "psmith@gmail.com",
                Telephone = "0851943376",
                Smoker = "No",
                SeriousIllness = "No",
            };
    
            return View(hooman);
        }
    

    Should be all you need, treat it as a string.