2016-06-10 143 views
1

我是新來的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 Vie w^

@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> 
} 

請幫助我,當我選擇從下拉列表中,按值提交按鈕,開機自檢功能不起作用,功能不增加價值的數據庫...謝謝你提前:)

回答

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

這與它完全沒有關係(並且對視圖模型使用'[Bind]'前綴是沒有意義的) –

相關問題