Asp.net Mvc - 添加到列表并立即保存

时间:2012-04-18 14:44:13

标签: c# jquery asp.net-mvc-3

我有一个联系信息,想要用户添加一些联系人然后保存它们。这意味着对于每个联系人我都不想将联系信息发送到服务器,我想将它们保存在客户端中并在保存后保存按钮然后所有这些都保存了。我怎么能这样做?如何添加到列表然后保存? 我的模型类是:

    public System.Guid ContactId { get; set; }
    public string Tel { get; set; }
    public string Fax { get; set; }
    public string Mobile { get; set; }

并在视图中:

            <% using (Html.BeginForm()) { %>
                <%: Html.ValidationSummary(true) %>
                                                    <div class="editor-label">
        <%: Html.LabelFor(model => model.Tel) %>
    </div>
                    <div class="editor-field">
                        <%: Html.EditorFor(model => model.Tel) %>
                        <%: Html.ValidationMessageFor(model => model.Tel) %>
                    </div>

                    <div class="editor-label">
                        <%: Html.LabelFor(model => model.Fax) %>
                    </div>
                    <div class="editor-field">
                        <%: Html.EditorFor(model => model.Fax) %>
                        <%: Html.ValidationMessageFor(model => model.Fax) %>
                    </div>

                    <div class="editor-label">
                        <%: Html.LabelFor(model => model.Mobile) %>
                    </div>
                    <div class="editor-field">
                        <%: Html.EditorFor(model => model.Mobile) %>
                        <%: Html.ValidationMessageFor(model => model.Mobile) %>
                    </div>
            <% using (Html.BeginForm()) { %>
                <%: Html.ValidationSummary(true) %>
                                            <div class="editor-label">
            <%: Html.LabelFor(model => model.Tel) %>
        </div>
                    <div class="editor-field">
                        <%: Html.EditorFor(model => model.Tel) %>
                        <%: Html.ValidationMessageFor(model => model.Tel) %>
                    </div>

                    <div class="editor-label">
                        <%: Html.LabelFor(model => model.Fax) %>
                    </div>
                    <div class="editor-field">
                        <%: Html.EditorFor(model => model.Fax) %>
                        <%: Html.ValidationMessageFor(model => model.Fax) %>
                    </div>

                    <div class="editor-label">
                        <%: Html.LabelFor(model => model.Mobile) %>
                    </div>
                    <div class="editor-field">
                        <%: Html.EditorFor(model => model.Mobile) %>
                        <%: Html.ValidationMessageFor(model => model.Mobile) %>
                    </div>
            <div class="demo">
                <button >
                    <span class="ui-button-text">Add to list</span></button>
            </div>
            <% List<Contact> list = (List<Contact>)ViewBag.ListContacts; %>
            <fieldset>
                <legend>Contacts</legend>
                <table>
                    <tr>
                        <th>
                            Tel
                        </th>
                        <th>
                            Fax
                        </th>
                        <th>
                            Mobile
                        </th>
                        <th>
                        </th>
                    </tr>
                    <% foreach (var itm in list)
                       { %>
                    <tr>
                        <td>
                            <%: Html.DisplayFor(f => itm.Tel) %>
                        </td>
                        <td>
                            <%: Html.DisplayFor(m => itm.Fax)%>
                        </td>
                        <td>
                            <%: Html.DisplayFor(m => itm.Mobile)%>
                        </td>
                        <td>
                            <%: Html.ActionLink("Delete","Delete",new {id= itm.ContactId}) %>
                        </td>
                    </tr>
                    <% } %>
                </table>
            </fieldset>
            <% } %>
            <% List<Contact> list = (List<Contact>)ViewBag.ListContacts; %>
            <fieldset>
                <legend>Contacts</legend>
                <table>
                    <tr>
                        <th>
                            Tel
                        </th>
                        <th>
                            Fax
                        </th>
                        <th>
                            Mobile
                        </th>
                        <th>
                        </th>
                    </tr>
                    <% foreach (var itm in list)
                       { %>
                    <tr>
                        <td>
                            <%: Html.DisplayFor(f => itm.Tel) %>
                        </td>
                        <td>
                            <%: Html.DisplayFor(m => itm.Fax)%>
                        </td>
                        <td>
                            <%: Html.DisplayFor(m => itm.Mobile)%>
                        </td>
                        <td>
                            <%: Html.ActionLink("Delete","Delete",new {id= itm.ContactId}) %>
                        </td>
                    </tr>
                    <% } %>
                </table>
            </fieldset>

            <div class="demo">
                <button >
                    <span class="ui-button-text">Save</span></button>
            </div>

            <% } %>

1 个答案:

答案 0 :(得分:0)

一个选项是创建javascript对象(http://docs.jquery.com/Types),然后当按下保存按钮时,您必须使用jQuery.each为每个项目发送jquery项目。

希望有所帮助