I've got a "ContactInfo" view in which I want to get a list of different phone numbers of different types from the user. The user should be able to add, edit and delete numbers and specify their types. At least one mobile number is required.
I've tried different approaches and I've come to a dead-end at some point. I thought describing the whole scenario and asking for a solution would be more efficient than to ask specific bugs. I'd be thankful for your answers.
Here is my phone class:
public partial class Phone
{
public int Id { get; set; }
public int UserId { get; set; }
public string Number { get; set; }
public PhoneNumberType Type { get; set; }
public virtual User User { get; set; }
}
and here is the phone type enum:
public enum PhoneNumberType
{
None,
Mobile,
Home,
Work
}
This is the model for the view (I have also included a list that populates phone types from the enum to fill the type combobox):
public class ContactInfoModel
{
public int Id { get; set; }
public string EmailAddress { get; set; }
public string Country { get; set; }
public string Province { get; set; }
public string County { get; set; }
public string City { get; set; }
public string Address { get; set; }
public ICollection<Phone> Phones { get; set; }
private IEnumerable<EnumMember> _phoneTypes;
public IEnumerable<EnumMember> PhoneTypes
{
get
{
if (this._phoneTypes == null)
{
_phoneTypes = EnumUtility.GetEnumMembers(typeof(PhoneNumberType));
}
return _phoneTypes;
}
}
}
And finally this is the view:
@model Jobeteria.Models.JobSeekers.ContactInfoModel
@{ViewBag.Title = "Contact Information";}
@using (Html.BeginForm()){
@Html.AntiForgeryToken()
<header class="pages-header">
<div class="pages-header-bg text-background"></div>
<hgroup>
<h1>@ViewBag.Title</h1>
</hgroup>
</header>
<div class="pages-wide-column">
<div class="pages-body-bg text-background"></div>
<div class="pages-content-marginer">
<fieldset>
<legend>Contact Information</legend>
<ol>
<li>
@*view to manipulate phone numbers*@
</li>
<li>
@Html.LabelFor(model => model.EmailAddress)
@Html.EditorFor(model => model.EmailAddress)
<br/>
@Html.ValidationMessageFor(model => model.EmailAddress)
</li>
<li>
@Html.LabelFor(model => model.Country)
@Html.EditorFor(model => model.Country)
<br/>
@Html.ValidationMessageFor(model => model.Country)
</li>
<li>
@Html.LabelFor(model => model.Province)
@Html.EditorFor(model => model.Province)
<br/>
@Html.ValidationMessageFor(model => model.Province)
</li>
<li>
@Html.LabelFor(model => model.County)
@Html.EditorFor(model => model.County)
<br/>
@Html.ValidationMessageFor(model => model.County)
</li>
<li>
@Html.LabelFor(model => model.City)
@Html.EditorFor(model => model.City)
<br/>
@Html.ValidationMessageFor(model => model.City)
</li>
<li>
@Html.LabelFor(model => model.Address)
@Html.TextAreaFor(model => model.Address)
<br/>
@Html.ValidationMessageFor(model => model.Address)
</li>
<li class="horizontal-field">
<input type="submit" value="SAVE" />
<input type="reset" value="Cancel" onclick="javascript: history.back(1);"/>
</li>
</ol>
</fieldset>
</div>
</div>}