选择下拉列表并保存到数据库asp.net mvc

时间:2016-06-10 13:48:38

标签: asp.net asp.net-mvc

我是asp.net mvc的新手,我找不到如何制作可选下拉列表的解决方案。我在我的项目中创建了这样的动态链接:

 ParentCategory (Category) - ChildCategory (SubCategory) - Pages (Pages List) - PageDetails (page details by id).

我的PageViewModels看起来像这样:

    public class ParentCategory
    {

        public int Id { get; set; }
        [Required]
        [Display(Name = "Parent Category Name")]
        public string PCatName { get; set; }
        [Required]
        [Display(Name = "UrlSeo")]
        public string UrlSeo { get; set; }
        [Required]
        [Display(Name = "Description")]
        public string PCatLogo { get; set; }

        public ICollection<ChildCategory> ChildCategories { get; set; }
    }

    public class ChildCategory
    {

        public int Id { get; set; }
        [Required]
        [Display(Name = "Parent Category Name")]
        public string CCatName { get; set; }
        [Required]
        [Display(Name = "UrlSeo")]
        public string UrlSeo { get; set; }
        [Required]
        [Display(Name = "Description")]
        public string Description { get; set; }

        public int ParentCategoryId { get; set; }

        public ICollection<Page> Pages { get; set; }
    }

    public class AddChildCategoryViewModel
    {
        public int Id { get; set; }
        public string CCatName { get; set; }
        public string UrlSeo { get; set; }
        public string Description { get; set; }

        public List<SelectListItem> ParentCategories { set; get; }
        public int ParentCategoryId { set; get; }

    }

和PageController

[HttpGet]
    public ActionResult AddNewCCat()
    {
        List<int> numlist = new List<int>();
        int num = 0;
        var Id = num;
        AddChildCategoryViewModel model = new AddChildCategoryViewModel();
        model.ParentCategories = context.ParentCategories.OrderBy(r => r.Id)
                            .Select(x => new SelectListItem
                            {
                                Value = x.Id.ToString(),
                                Text = x.PCatName
                            }).ToList();
        model.Id = Id;
        return View(model);
    }


    // [Authorize(Roles = "Admin")]
    [HttpPost]
    [ValidateAntiForgeryToken]
    [ValidateInput(false)]
    public ActionResult AddNewCCat(AddChildCategoryViewModel model)
    {
        var ccategory = new ChildCategory
        {
            Id = model.Id,
            CCatName = model.CCatName,
            UrlSeo = model.UrlSeo,
            Description = model.Description,
            ParentCategoryId = model.ParentCategoryId
        };
        _pageRepository.AddNewChildCategory(ccategory);
        return RedirectToAction("Index", "Page");
    }

这是我的PageRepository

 public void AddNewChildCategory(ChildCategory ccategory)
    {
        _context.ChildCategories.Add(ccategory);
        Save();
    }

这是AddNewCCat View

@model bandymas.Models.PageViewModels.AddChildCategoryViewModel
@using bandymas.Controllers;
@using bandymas.Models;
@using Microsoft.AspNet.Identity
@{
ViewBag.Title = "Add New CCat";
Layout = "~/Views/Shared/_Layout.cshtml";
PageController pageCtrl = new PageController();

}
@section Scripts
{
<script src="~/Scripts/view.js"></script>
<script src="~/ckeditor/ckeditor.js"></script>
}

@using (Html.BeginForm("AddNewCCat", "Page", FormMethod.Post, new { role = "form" }))
{
@Html.AntiForgeryToken()
<div class="editPostContainer">
    <table>
        @*<tr>
                <td>Id :</td>
                <td colspan="2" class="editPageId">@Html.TextBoxFor(m => m.Id, new { @class = "editIdInp", @readonly = "readonly" })</td>
            </tr>*@
        <tr>
            <td>CCatName :</td>
            <td colspan="2" class="editPageUserName">@Html.TextBoxFor(m => m.CCatName, new { @class = "editCCatNameInp" })</td>
        </tr>
        <tr>
            <td>UrlSeo :</td>
            <td colspan="2">@Html.TextBoxFor(m => m.UrlSeo, new { @class = "editUrlSeo" })</td>
        </tr>
        <tr>
            <td>Description :</td>
            <td colspan="2">@Html.TextBoxFor(m => m.Description, new { @class = "editDescription" })</td>
        </tr>
        <tr>
            <td>Select Parent Category :</td>
            <td>@Html.DropDownListFor(m => m.ParentCategoryId, Model.ParentCategories, "--Select One--")</td>
        </tr>
        <tr>
            <td colspan="3" class="editPageBody"><input class="comTextBtn" type="submit" value="&#x27a5;" /></td>
        </tr>
    </table>
</div>
}

请帮帮我,当我从下拉列表中选择值并按下提交按钮时,POST功能不起作用,功能不对数据库增值...提前谢谢:)

1 个答案:

答案 0 :(得分:0)

public ActionResult AddNewCCat([Bind(Include = "Id, CCatName, UrlSeo, Description, ParentCategoryId")] AddChildCategoryViewModel model)      
{     
    //Code save//
}