2

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>}
4

1 回答 1

0

我总是发现模型绑定到 Enumerable 项目有点痛苦 - 这基本上是你想要实现的权利,允许用户编辑电话号码列表?

在验证方面,我会编写一个自定义验证属性以在您的视图模型中使用 - 这将检查是否存在至少一个电话号码。

Phil Haack 曾在博客上写过关于绑定到列表的文章:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

改编自 Phil 的帖子,我认为您将不得不这样做:

<% for (int i = 0; i < userPhoneNumeberCount; i++) { %>

  <%: Html.TextBoxFor(m => m.Phones[i].Number ) %>
  <%: Html.SelectListFor(m => m.Phones[i].Type, PhoneNumberSelectListData ) %> 
<% } %>
于 2012-11-26T13:04:33.370 回答