Kra*_*mir 0 c# razor c#-4.0 asp.net-mvc-4
我在这里搜索了很多,并找到这样的东西来制作下拉列表
这是我的控制器:这传递我的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");
}
Run Code Online (Sandbox Code Playgroud)
我的看法 :
@model WebHFM.Models.Profits
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Profits</legend>
<div class="editor-field">
@Html.DropDownList("Id", (SelectList) ViewBag.Id, "--Select One--")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CategoryName.Id)
@Html.ValidationMessageFor(model => model.CategoryName.Id)
</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>...
Run Code Online (Sandbox Code Playgroud)
这个数据插入到数据库但是CategoryName_Id是NULL我错过了什么?和CategoryName = profits.CategoryName这是利润中类别的外键 public Categories CategoryName { get; set; }
在你的Id上调用.ToString()
model.CategoryMenu = db.Categories.Select(c => new SelectListItem {
Text = c.Name,
Value = c.Id.ToString(),
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
391 次 |
| 最近记录: |