从多级动态集合中添加下拉列表

程序员101

我有这些模型。

项目视图模型

public class ProjectViewModel
    {
        //some properties..

        public IList<ScopeOfWorkViewModel> ScopeOfWork { get; set; }
    }

工作范围视图模型

public class ScopeOfWorkViewModel
    {
        //some properties
        public IList<MaterialsViewModel> Materials { get; set; }
    }

材料视图模型

 public class MaterialsViewModel
        {
            public int MaterialId { get; set; }
            [Display(Name = "Material")]
            public string MaterialName { get; set; }
            public int? Quantity { get; set; }
            public double? Cost { get; set; }
            public int? ScopeOfWorkId { get; set; }

            // should be a drop down list and get its data from database
            public IEnumerable<SelectListItem> CategoryList { get; set; }
        }

类别模型

 public partial class tblCategory
    {
        public tblCategory()
        {
            this.tblMaterials = new HashSet<tblMaterial>();
        }

        public int CategoryId { get; set; }
        public string CategoryName { get; set; }

        public virtual ICollection<tblMaterial> tblMaterials { get; set; }
    }

这是我的观点

@model MigratingDB.Models.ProjectViewModel
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal" id="ProjectForm">
        <h4>ProjectViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        Dynamically add ScopeOfWork and Materials<br />
        <a href="javascript:void(0)" id="addScope">Add Scope of Work</a>
        <div id="scopes">
            <div class="scope">
                <div class="materials">
                    <div class="material">
                    </div>
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>


<div id="newScope" style="display:none">
    <div class="scope">
        <h3>Scope</h3>
        <div class="form-group">
            <label for="_#__ScopeOfWorkName" class="control-label col-md-2">Scope Of Work</label>
            <div class="col-md-10">
                <input class="form-control" type="text" id="_#__ScopeOfWorkName" name="ScopeOfWork[#].ScopeOfWorkName" value="">
            </div>
        </div>
        <input type="hidden" class="scopeindex" name="ScopeOfWork.Index" value="#" />

        <div class="materials">
            <h4>Material</h4>
            <a href="javascript:void(0)" class="addmaterial">Add Material</a>
        </div>
    </div>
</div>

<div id="newMaterial" style="display:none">
    <div class="form-group">
        <label for="_#__Materials_%__MaterialName" class="control-label col-md-2">Material</label>
        <div class="col-md-2">
            <input class="form-control" type="text" id="_#__Materials_%__MaterialName" name="ScopeOfWork[#].Materials[%].MaterialName" value="">
        </div>
    </div>

    <div class="form-group">
        <label for="_#__Materials_%__Quantity" class="control-label col-md-2">Quantity</label>
        <div class="col-md-1">
            <input class="form-control" type="text" id="_#__Materials_%__Quantity" name="ScopeOfWork[#].Materials[%].Quantity" value="">
        </div>
    </div>

    <div class="form-group">
        <label for="_#__Materials_%__Cost" class="control-label col-md-2">Cost</label>
        <div class="col-md-1">
            <input class="form-control" type="text" id="_#__Materials_%__Cost" name="ScopeOfWork[#].Materials[%].Cost" value="">
        </div>
    </div>

//build a category drop down list here

    <input type="hidden" class="materialindex" name="ScopeOfWork[#].Materials.Index" value="%" />
</div>

<script>
    var form = $('form');
    var scope = $('#newScope');
    var material = $('#newMaterial');

    form.on('click', '.addmaterial', function () {
        var clone = material.clone();
        var scopeIndex = $(this).closest('.scope').find('.scopeindex').val();
        clone.html($(clone).html().replace(/#/g, scopeIndex));

        var materialIndex = new Date().getTime();
        clone.html($(clone).html().replace(/%/g, materialIndex));
        $(this).closest('.materials').append(clone.html());
        form.data('validator', null);
        $.validator.unobtrusive.parse(form);
    });

    $('#addScope').click(function () {
        var clone = scope.clone();
        var scopeIndex = new Date().getTime();
        clone.html($(clone).html().replace(/#/g, scopeIndex));
        $('#scopes').append(clone.html());
        form.data('validator', null);
        $.validator.unobtrusive.parse(form);
    });
</script>

每次创建 MaterialViewModel 对象并将其发布到我的数据库时,请帮助我为该类别构建一个下拉列表。我有一个类别表,我想在其中传递其值以生成下拉列表。我更喜欢使用强类型模型而不是使用 ViewBag

用户3559349

ProjectViewModel需要一个属性IEnumerable<SelectListItem>(或一个IEnumerable包含选项值和文本的 2 个属性的类),以便您可以将选项值传递给视图(如果没有现有的MaterialsViewModel)。MaterialsViewModel还需要一个属性来绑定所选类别。

public class ProjectViewModel
{
    // some properties..
    public IEnumerable<SelectListItem> CategoryList { get; set; }
    public IList<ScopeOfWorkViewModel> ScopeOfWork { get; set; }
}
....
public class MaterialsViewModel
{
    public int MaterialId { get; set; }
    ....
    // public int? ScopeOfWorkId { get; set; } this is not required
    public int SelectedCategory { get; set; }
    public IEnumerable<SelectListItem> CategoryList { get; set; }
}

然后在 GET 方法中,初始化您的视图模型并填充CategoryList,例如

IEnumerable categories = db.tblCategory; // select all categories
ProjectViewModel model = new ProjectViewModel()
{
    CategoryList = new SelectList(categories, "CategoryId", "CategoryName"),
    .... set other properties as required
};
return View(model);

在视图中,包含一个将 转换CategoryList为 javascript 数组的脚本

<script>
    var form = $('form');
    ....
    var categories = @Html.Raw(Json.Encode(Model.CategoryList));

    form.on('click', '.addmaterial', function () {
        ....

然后在您的模板中(在 ` 元素中),为选择控件添加 html(但如果需要,除了标签选项之外没有任何选项)。请注意用于选择的附加类名称。

<div class="form-group">
    <label for="_#__Materials_%__SelectedCategory" class="control-label col-md-2">Cost</label>
    <div class="col-md-1">
        <select class="form-control category" id="_#__Materials_%__SelectedCategory" name="ScopeOfWork[#].Materials[%].SelectedCategory">
            <option value="">Please select a category</option>
        </select>
    </div>
</div>

并修改您的脚本以根据 javascript 数组中的值添加选项。

form.on('click', '.addmaterial', function () {
    var clone = material.clone();
    ....
    clone.html($(clone).html().replace(/%/g, materialIndex));

    // Add the options
    var select = clone.find('.category');
    $.each(categories, function(index, item) {
        select.append($('<option></option>').val(item.Value).text(item.Text));
    });

    $(this).closest('.materials').append(clone.html());
    form.data('validator', null);
    $.validator.unobtrusive.parse(form);
});

请注意,您没有为现有项目呈现任何 html,因此如果任何内容无效并且您返回视图,则用户动态添加的所有数据都将丢失 - 您需要嵌套for循环。并且您不渲染 html 以对动态添加的元素进行验证。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

动态添加下拉列表并将数据存储在json中

来自分类Dev

从json动态添加下拉列表

来自分类Dev

增加类型号后动态添加下拉列表

来自分类Dev

Tkinter:动态添加下拉列表并获取它们的值

来自分类Dev

动态添加下拉值

来自分类Dev

要添加下拉列表

来自分类Dev

如何使用Flutter Web在列表视图中动态添加下拉按钮或删除下拉列表?

来自分类Dev

如何使用vbs在excel中添加下拉列表

来自分类Dev

如何在表格中添加下拉列表?

来自分类Dev

如何在从服务器获取数据的动态表的每一行中添加下拉列表?

来自分类Dev

为django添加下拉列表

来自分类Dev

如何在GridView单元格内动态添加下拉列表

来自分类Dev

在laravel中动态添加下拉框。Javascript和laravel

来自分类Dev

在下拉列表中动态添加值

来自分类Dev

使用C#在Excel工作表中添加下拉列表

来自分类Dev

如何根据数据库中存在的数字添加下拉列表

来自分类Dev

如何通过自定义加载项在 Outlook 会议请求中添加下拉列表?

来自分类Dev

如何在react js中更改另一个下拉值时动态添加下拉值?

来自分类Dev

如何添加下拉列表默认值

来自分类Dev

如何使用PHPExcel添加下拉列表控件

来自分类Dev

从服务器端添加下拉列表

来自分类Dev

添加下拉列表与其按钮之间的距离

来自分类Dev

SweetAlert下拉列表可动态添加列表中的项目

来自分类Dev

PHP-从数组中添加下拉选项

来自分类Dev

没有在ajax回调中添加下拉值

来自分类Dev

如何在HTML中添加下拉菜单?

来自分类Dev

使用HTML在表格中添加下拉菜单

来自分类Dev

如何在 HTML 表单中添加下拉菜单?

来自分类Dev

webforms:在javascript选项中动态添加到下拉列表

Related 相关文章

热门标签

归档