[英]mvc4 dropdownList insert NULL
我在這里搜索了很多東西,找到了類似的東西作為下拉列表
這是我的模型:
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
為NULL
我缺少什么? 和CategoryName = profits.CategoryName
這是Profits public Categories CategoryName { get; set; }
的外鍵public Categories CategoryName { get; set; }
public Categories CategoryName { get; set; }
修改您的利潤類別,添加以下內容:
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。
您的Create()動作返回return View();
沒有將模型傳遞給視圖方法參數。
這是我不看您的模型所建議的。 PopulateCategoriesDropDownList與您的create動作位於同一控制器中
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/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.