在 asp.net mvc 2 中使用 jQuery 在表单发布之前动态构建集合

发布于 2024-10-02 15:39:17 字数 387 浏览 2 评论 0原文

在我的对象图中,我有一个标准的父+子关系。可以这样说:

Cat.Kittens = new HashSet();

当我有一个新猫的表单时,我想在单击“保存”按钮之前添加零只或多只小猫。由于它是一只新猫,我也不想将小猫发布到服务器(即使用 AJAX 等;我只想使用保存按钮将一个大转储发送到服务器)。我正在尝试找出有效的最佳方法,但谷歌的能力不足。一定有人已经这么做了。

我不太关心父猫是否是新的;如果我必须将小猫添加推到编辑表单中,那就这样吧。对我来说,问题是如何在客户端上动态添加多只小猫,然后能够在存储数据之前在控制器中处理集合。

有什么想法吗?好的例子吗? jQuery + asp.net mvc 2 是堆栈。

提前致谢!

In my object graph, I have a standard parent + children relationship. Say something like,

Cat.Kittens = new HashSet<Cat>();

When I have a form for a new Cat, I want to add zero-or-many kittens prior to clicking the save button. Since it's a new Cat, I also don't want to post the kitten to the server yet (i.e. using AJAX or the like; I just want to do one big dump to the server with the save button). I'm trying to figure out the best way to do this with some efficiency but am coming up short on Google. Someone has to have done this already.

I'm less concerned about whether the parent Cat is new or not; if I have to push the kitten addition off to an Edit form, so be it. The issue for me is how to dynamically add multiple kittens on the client and subsequently be able to massage the collection in the controller prior to storing the data.

Any ideas? Good examples? jQuery + asp.net mvc 2 is the stack.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2024-10-09 15:39:17

它可以用一种形式来完成,我已经这样做了,我将在页面上隐藏一个空白的小猫输入表单,然后当客户说添加另一只小猫时用 jquery 复制它。
控制器是比较困难的地方

public class Cat
{
    public string name { get; set; }
    public List<Kitten> Kittens { get; set; }

}

public class Kitten
{
    public int id { get; set; }
    public string color { get; set; }
    public string CatName { get; set; }
}

Controller

       public ActionResult AddCat()
    {
        return View();
    }

    [HttpPost]
    public ActionResult AddCat(Cat c)
    {
        var sb = new StringBuilder();
        sb.Append(c.name);
        sb.Append("kittens are : " + Request.Form["color"]);
        return Content(sb.ToString());
    }

View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Kittens.Models.Cat>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
AddCat
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<h2>
    AddCat</h2>
<% using (Html.BeginForm())
   {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>Fields</legend>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.name) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.name) %>
        <%: Html.ValidationMessageFor(model => model.name) %>
    </div>
    <table id="kittenDest">
        <tr>
            <td colspan="2">
                <input id="Button1" type="button" value="add Kitten" />
            </td>
        </tr>
    </table>
    <p>
        <input type="submit" value="Create" />
    </p>
    <table id="kittenForm" style="display: none;">
    <tr>
        <td>                
            <input id="color" name="color" value="" type="text" />
        </td>
    </tr>
</table>
</fieldset>
<% } %>
<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#Button1").click(function () {
            $("#kittenDest").append($("#kittenForm").html());
        });
    });
</script>

It can be done in one form I have done this where I will keep a blank kitten input form hidden on the page then copy it with jquery when the client says add another kitten.
The controller is where it gets a little tougher

public class Cat
{
    public string name { get; set; }
    public List<Kitten> Kittens { get; set; }

}

public class Kitten
{
    public int id { get; set; }
    public string color { get; set; }
    public string CatName { get; set; }
}

Controller

       public ActionResult AddCat()
    {
        return View();
    }

    [HttpPost]
    public ActionResult AddCat(Cat c)
    {
        var sb = new StringBuilder();
        sb.Append(c.name);
        sb.Append("kittens are : " + Request.Form["color"]);
        return Content(sb.ToString());
    }

View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Kittens.Models.Cat>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
AddCat
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<h2>
    AddCat</h2>
<% using (Html.BeginForm())
   {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>Fields</legend>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.name) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.name) %>
        <%: Html.ValidationMessageFor(model => model.name) %>
    </div>
    <table id="kittenDest">
        <tr>
            <td colspan="2">
                <input id="Button1" type="button" value="add Kitten" />
            </td>
        </tr>
    </table>
    <p>
        <input type="submit" value="Create" />
    </p>
    <table id="kittenForm" style="display: none;">
    <tr>
        <td>                
            <input id="color" name="color" value="" type="text" />
        </td>
    </tr>
</table>
</fieldset>
<% } %>
<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#Button1").click(function () {
            $("#kittenDest").append($("#kittenForm").html());
        });
    });
</script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文