0

我在这里搜索了很多,找到了这样的东西来制作下拉列表
,这是我的模型:

public class Profits
    {
        [Key]
        public int Id { get; set; }
        public double Value { get; set; }
        public string Description{ get; set; }
       [DataType(DataType.Date)]
        public DateTime DateInput { get; set; }
        public UserProfile User { get; set; }
        public Categories CategoryName { get; set; }//foreign key
          public   int CategoryID { get; set; }
    }

 public class Categories
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string  Name { get; set; }
        public UserProfile User { get; set; }


    }

这是我的控制器:这会将我的数据传递给 dropDownList...

public ActionResult Create()
{
    var dba = new WHFMDBContext();
    var query = dba.Categories.Select(c => new { c.Id, c.Name });
    ViewBag.Id = new SelectList(query.AsEnumerable(), "Id", "Name", 3);
    return View();
}

[HttpPost]
        [InitializeSimpleMembership]
        public ActionResult Create(Profits profits)
        {
            var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
            var profit = new Profits
            {
               Value= profits.Value,
               Description = profits.Description,
               DateInput =profits.DateInput,
               CategoryName =profits.CategoryName,// ???
                User = user,

            };
            db.Profits.Add(profit);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

我的观点 :

@model WebHFM.Models.Profits



@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Profits</legend>


    <div class="editor-field">
     @Html.LabelFor(model => model.CategoryName)
        </div>
    <div class="editor-field">
         @Html.DropDownList("Id", (SelectList) ViewBag.Id, "--Select One--") 
        @Html.ValidationMessageFor(model => model.CategoryName)
    </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Value)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Value)
            @Html.ValidationMessageFor(model => model.Value)
        </div>...

这将数据插入数据库,但我错过了什么CategoryName_Id?这是利润中类别的外 NULLCategoryName = profits.CategoryNamepublic Categories CategoryName { get; set; }

4

3 回答 3

1

修改您的利润等级,添加以下内容:

Profits
{
  int CategoryID {get;set;}
}

修改你的cshtml。改变

@Html.DropDownList("Id", (SelectList) ViewBag.Id, "--Select One--") 

@Html.DropDownList("CategoryID", (SelectList) ViewBag.Id, "--Select One--") 

改变你的控制器:

public ActionResult Create(Profits profits)
        {
            var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
            var category = db.Categories.FirstOrDefault(o => o.CategoryId == profits.CategoryID);
            var profit = new Profits
            {
               Value= profits.Value,
               Description = profits.Description,
               DateInput =profits.DateInput,
               CategoryName = category,
               User = user
            };
            db.Profits.Add(profit);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

这将更改您的下拉列表以返回选定的 CategoryID。标记将如下所示:

<select name="CategoryID" />

在您的回发中,这将绑定到您的新 Profits.CategoryID。然后,您使用它进行数据库查询以获取选定的类别对象,然后将其分配给 Profits.CategoryName。

于 2013-03-05T20:34:30.200 回答
0

您的 Create() 操作返回return View();而不将模型传递给视图方法参数。

于 2013-03-05T20:09:58.103 回答
0

这是我不看你的模型的建议。PopulateCategoriesDropDownList 与您的创建操作位于同一控制器中

private void PopulateCategoriesDropDownList(object selectedCategories = null)
    {
        var categoriesQuery = from d in _dataSource.Categories
                         orderby d.Name
                         select d;
        ViewBag.CategoryId= new SelectList(categoriesQuery, "CategoryId", "Name", selectedCategories);
    }

然后你的行为是这样的。

    [HttpGet]
    public ActionResult Create()
    {
      PopulateCategoriesDropDownList();
    return View();
   }


    [HttpPost]
    [InitializeSimpleMembership]
    public ActionResult Create(Profits profits)
    {
         var user = db.UserProfiles.FirstOrDefault(x => x.UserId ==       WebSecurity.CurrentUserId);
        var profit = new Profits
        {
           Value= profits.Value,
           Description = profits.Description,
           DateInput =profits.DateInput,
           CategoryName =profits.CategoryName,// ???
            User = user,

        };
        db.Profits.Add(profit);
        db.SaveChanges();
        PopulateCategoriesDropDownList(profit.CategoryId);
       return View(profit);
    }

在您看来:

<div class="editor-label">
            @Html.LabelFor(model => model.CategoryId, "Pick Category")
    </div>
    <div class="editor-field">
        @Html.DropDownList("CategoryId", String.Empty)
        @Html.ValidationMessageFor(model => model.CategoryId)
    </div><br/>
于 2013-03-05T22:04:16.943 回答