MVC 3 - 渲染为儿童对象的其他编辑器模板 [英] MVC 3 - Render additional Editor templates for children objects

查看:128
本文介绍了MVC 3 - 渲染为儿童对象的其他编辑器模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个表单,允许用户与0多个孩子创建一个实体对象,主要的对象是一个有0〜许多受益人付款人的对象。三者之间的关系如下:

I'm trying to create a form that allows the user to create an an entity with 0-multiple children objects, the main object being a Lead which has 0-many Beneficiary and Payer objects. The relation between the three is as follows:

Lead has many Beneficiaries (one to many)
Lead has many Payers (many to many) joined through LeadsPayers

在我的编辑对我的铅视图模型我有一个为每个受益人和纳税人附着在引编辑器中循环,但我希望能有一个按钮,将动态地添加其他受益人或付款人的铅和使用编辑模板,以便我没有在我的javascript code复制HTML。

In my editor for my lead view model I have a loop that generates the editor for each beneficiary and payer attached to the lead but I want to be able to have a button that will dynamically add another beneficiary or payer to the lead and use the editor template so i don't have duplicated html in my javascript code.

我有这方面的工作结合一切的时,我得到的形式发布到控制器我只是不知道如何再利用编辑模板code生成与我目前的形式兼容HTML,有没有人这样做过?必须有比一个Ajax按钮一种更好的方式呈现的HTML

I have this working to bind everything to the Lead when my form gets posted to the controller I just don't know how to reuse the editor template code to generate compatible html with my current form, has anyone done this before? There has to be a better way than an ajax button that renders the html

推荐答案

<一个href=\"http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/\">This帮助我在正确的轨道但不完全是我想要做的,因为这似乎并​​不容易MVC支持的,这里是我的工作四处得到这一切与控制器模型绑定工作。

This helped me get on the right track but wasn't fully what I wanted to do and as this doesn't seem easily supported in MVC here is my work around to get this all to work with model binding in the controller.

要动态地添加子对象与适当的HTML,这样传递给控制方法时,他们会自动绑定到父窗体,这意味着我需要HTML这样的

To dynamically add children objects to a form with the proper html so that they would be auto bound to the parent when passed to the control method, meaning I needed HTML like this

<input name='Lead_Beneficiaries_1__{Property}' id=... />

和不喜欢这样(这是博客文章的结果)

And not like this (which is what the blog post results in)

<input name='Beneficiary__{Property}' />

在发布到控制器,自动绑定不父。

Which doesn't auto bind to the parent when posted to the controller.

代替使用Ajax来呈现来自控制器的编辑模板的HTML时的形式被加载,并根据需要由用户然后更可以装载,我自动呈现一个编辑的模板。 JQuery的code

Instead of using Ajax to render the editor template html from the controller, I auto render one editor template when the form is loaded and then more can be loaded as needed by the user. JQuery code

$("#addBeneficiary").click(function(e) {
    e.preventDefault();
    var beneficiary = $(".beneficiary:last").clone();
    var count = parseInt(beneficiary.find("input:first").attr("id").match(/\d+/), 10);
    beneficiary.find("input").each(function(indx, element) { 
        var name = $(element).attr('name').replace(count, count+1),
            id   = $(element).attr('id').replace(count, count+1);
        $(element).attr('name', name);
        $(element).attr('id', id);
        $(element).val(''); 
    });
    $("#beneficiaries").append(beneficiary);
});

我只是包裹在一个div我的编辑模板,以便它可以很容易抓住作为一个整体,从jQuery的,克隆它,清空输入的值,并增加每个元素的柜台上,贴的时候我让孩子实体的集合我的父母,而无需显式地抓住他们的方法或从字典中的FormCollection拉他们。

I just wrapped my editor template in a div so it can be easily grabbed as a whole from jquery, cloned it, emptied the values of the inputs and incremented the counter for each element so when posted I get a collection of children entities on my parent without having to explicitly catch them in the method or pull them from the formcollection dictionary.

虽然这可能不是最好的解决方案,它得到我预期的方式来完成工作,我最终没有复制的HTML编辑器模板。此外,如果形式错误,因为父对象有多个孩子,他们让我的形式code和没有自动呈现汽车,是因为客户端新增的丢失。

While this probably isn't the best solution, it gets the job done in the way I intended and I didn't end up duplicating HTML for the editor template. Also if the form errors since the parent object has the multiple children they get auto rendered automatically by my form code and nothing is lost because of client side additions.

这篇关于MVC 3 - 渲染为儿童对象的其他编辑器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆