0


Sorry because my English not good! But I have a problem like this
1. I create a example using MVC3 to manage Menu Multil Level and Table Menu like this
- MenuWeb(MenuId,Name,Url,ParentId)
2. Function CreateMenu in Controller:

[HttpGet]
        public ActionResult CreateMenu()
        {
            ViewBag.MenuId = new SelectList(db.MenuWebs.ToList(), "MenuId", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult CreateMenu(MenuWeb menu)
        {
            if (ModelState.IsValid)
            {
                db.MenuWebs.InsertOnSubmit(menu);
                db.SubmitChanges();
                return RedirectToAction("../admin/getallmenu");
            }
            return View();
        }

3. Form CreateMenu in CreateMenu.cshtml like this:

@model MyBlog.Models.MenuWeb
@{
    ViewBag.Title = "CreateMenu";
}

    @using (Html.BeginForm())
    {
        <fieldset>
            <legend>Create menu</legend>

            <div class="div-lable">
                Menu Parent:
            </div>
            <div class="div-editor">
                @Html.DropDownList("MenuId")
            </div>
            <div class="div-lable">
                Name:
            </div>
            <div class="div-editor">
                @Html.EditorFor(model => model.Name)
            </div>
            <div class="div-lable">
                Url Redirect:
            </div>
            <div class="div-editor">
                @Html.EditorFor(model => model.UrlRedirect)
            </div>
            <div class="div-lable">
                Order:
            </div>
            <div class="div-editor">
                @Html.EditorFor(model => model.Ordes)
            </div>
            <div class="div-editor">
                <input type="submit" value="Create Menu" />
            </div>
            <div class="div-editor">
                @Html.ActionLink("Back to list", "getallmenu")
            </div>
        </fieldset>
    }

And in this example. I don't know to set MenuId from Dropdowlist into ParentId? I try submit but ParentId alway is null. Please help me! Thanks so murch.

4

1 に答える 1

0

okay change your code something like this :

Controller


Replace this

ViewBag.MenuId = new SelectList(db.MenuWebs.ToList(), "MenuId", "Name");

With

ViewBag.MenuIds = new SelectList(db.MenuWebs.ToList(), "MenuId", "Name");


View


Replace this

@Html.DropDownList("MenuId")

With

@Html.DropDownListFor(model => model.ParentId, (SelectList)ViewBag.MenuIds)

This will fix your issue.

于 2012-11-30T16:36:24.813 に答える