3

再帰関数を使用して文字列のリストを使用してドロップダウンをバインドします
ドロップダウンには、ホームホーム>>キッチンホーム>>キッチン>> ABCのような値があります

そして、データベース内の同じドロップダウン値ABCが必要です

これは私のビューコードです

@{
    ViewBag.Title = "Createnewproduct";
}
<h2>
    Create new product</h2>
<div>
    @using (Html.BeginForm("Createnewproduct", "ProductAdmin", FormMethod.Post, new { id = "sendFileForm", enctype = "multipart/form-data" }))
    {
        <table>
            <tr>
                <td>
                    Category
                </td>
                <td>
                    @Html.DropDownList("Test", new SelectList(ViewBag.ListOfDisciplines, Model))
                </td>
            </tr>
            <tr>
                <td>
                    Product Name
                </td>
                <td>
                    <input type="text" name="ProductName">
                </td>
            </tr>
            <tr>
                <td>
                    Product Description
                </td>
                <td>
                    <input type="text" name="ProductDescription">
                </td>
            </tr>
            <tr>
                <td>
                    Product long Description
                </td>
                <td>
                    <input type="text" name=" ProductlongDescription">
                </td>
            </tr>
            <tr>
                <td>
                    UPC
                </td>
                <td>
                    <input type="text" name="UPC">
                </td>
            </tr>
            <tr>
                <td>
                    SKU
                </td>
                <td>
                    <input type="text" name="SKU">
                </td>
            </tr>
            <tr>
                <td>
                    Stock
                </td>
                <td>
                    <input type="text" name="Stock">
                </td>
            </tr>
            <tr>
                <td>
                    Weight
                </td>
                <td>
                    <input type="text" name="Weight">
                </td>
            </tr>
            <tr>
                <td>
                    Height
                </td>
                <td>
                    <input type="text" name="Height">
                </td>
            </tr>
            <tr>
                <td>
                    Image URL
                </td>
                <td>
                    <input type="file" name="file" id="file" style="width: 100%;" id="Imageupload" />
                </td>
            </tr>
            <tr>
                <td rowspan="2">
                    <input type="submit" value="Save" />
                </td>
            </tr>
        </table>
    }
</div>

そして私のコントローラーはこのように

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Createnewproduct(FormCollection form)
        {

        }

そこに FormCollection の値が表示されると、ドロップダウン値が見つかりませんでした。他のすべての値は FormCollection で適切に見つかります

このコードのどこで間違いを犯したか教えてください


 public ActionResult Createnewproduct()
    {
        List<Category> categorylist = _Listofcategory();
        var parentcate = categorylist.Where(c => c.ParentCategoryId == 1).ToList();
        List<String> categoryList = new List<string>();
        string prefix = ">>";
        foreach (var item in parentcate)
        {
            prefix = item.CategoryName;
            Setchild(prefix, item, categorylist, categoryList);


        }

        ViewBag.ListOfDisciplines = categoryList;
        return View();
    }


    private void Setchild(string prefix, Category model, List<Category> listcategory, List<string> catStrings)
    {
        var childs = listcategory.Where(x => x.ParentCategoryId == model.CategoryId).ToList();

        catStrings.Add(prefix);
        if (childs.Count > 0)
        {

            foreach (var child in childs)
            {
                catStrings.Add(prefix + ">>" + child.CategoryName);
                var subchild = listcategory.Where(c => c.ParentCategoryId == child.CategoryId).ToList();
                if (subchild.Count > 0)
                {
                    foreach (var subsubchild in subchild)
                    {
                        catStrings.Add(prefix + ">>" + child.CategoryName + ">>" + subsubchild.CategoryName);

                        var subsubsubchild = listcategory.Where(c => c.ParentCategoryId == subsubchild.CategoryId).ToList();
                        if (subsubsubchild.Count > 0)
                        {
                            foreach (var subsubsubsubchild in subsubsubchild)
                            {
                                catStrings.Add(prefix + ">>" + child.CategoryName + ">>" + subsubchild.CategoryName + ">>" + subsubsubsubchild.CategoryName);
                            }
                        }
                    }
                }


            }

        }

    }

これは、ビューモデルでの使用方法のカテゴリのリストです。

私にお知らせください

4

2 に答える 2

0

ViewBag.ListOfDisciplinesあなたが でList<Discipline>あり、あなたの Discipline クラスが次のとおりであると仮定します。

public class Discipline {
     public int Id { get; set; }
     public string Name { get; set; }
}

文字列のリストがある場合は、コントローラー側で匿名の配列に変換できます

ViewBag.ListOfDIsciplines = from d in ListDisciplines select new { Id = d, Name = d };

あなたの見解では、使用する必要があります

@Html.DropDownList("Test", new SelectList(ViewBag.ListOfDisciplines, "Id", "Name"), "-- Select here --")
于 2013-08-31T14:34:53.617 に答える