Asp.net mvc Asp.Net MVC dropdownlist未向控制器发布值->;数据库
我的开发环境是ASP.NET MVC 5,使用Entity Framework 6,使用代码优先工作流 我的问题是:当尝试向数据库添加新记录时,我的两个dropdownlists中的值没有被包括在内。所有其他字段实际上都保存到数据库中 我正在将ViewModel传递给相关视图:Asp.net mvc Asp.Net MVC dropdownlist未向控制器发布值->;数据库,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我的开发环境是ASP.NET MVC 5,使用Entity Framework 6,使用代码优先工作流 我的问题是:当尝试向数据库添加新记录时,我的两个dropdownlists中的值没有被包括在内。所有其他字段实际上都保存到数据库中 我正在将ViewModel传递给相关视图: public class NewItemViewModel { public IEnumerable<Category> Categories { get; set; } public Ite
public class NewItemViewModel
{
public IEnumerable<Category> Categories { get; set; }
public Item Item { get; set; }
public IEnumerable<Donor> Donors { get; set; }
}
相关的控制器:这个New()操作只是用来将数据传递给要提交表单的视图
public ActionResult New()
{
var itemCategories = _context.ItemCategory.ToList();
var donors = _context.Donors.ToList();
var viewModel = new NewItemViewModel
{
Categories = itemCategories,
Donors = donors
};
return View(viewModel);
}
以下是我将提交的数据添加到数据库的实际操作:
// Create a new item.
[HttpPost]
public ActionResult Create(Item item)
{
_context.Items.Add(item);
_context.SaveChanges();
return RedirectToAction("Index", "Item");
}
最后,视图本身(我将在这里最小化代码):
@使用(Html.BeginForm(“创建”、“项目”))
{
@LabelFor(m=>m.Item.Donor)
@DropDownListFor(m=>m.Item.Donor,新选择列表(Model.donders,“Id”,“FullName”),“谁捐赠了这个项目?”,新{@class=“form control”})
@LabelFor(m=>m.Item.Category)
@DropDownListFor(m=>m.Item.Category,新选择列表(Model.Categories,“Id”,“Name”),“选择项类别”,新{@class=“form control”})
}
重申我遇到的问题:类别和捐赠者的dropdownlist的值没有保存到数据库中,而其他(非导航属性?)的名称、描述、数量等工作正常
我使用ViewModel的方式正确吗?我的印象是MVC框架知道如何处理在Create()操作中传递Item对象参数的问题——映射它在Item实体中需要什么
任何帮助都将不胜感激。谢谢。在NewItemViewModel中,您尚未创建用于保存下拉列表中选定值的属性
public class NewItemViewModel
{
public IEnumerable<Category> Categories { get; set; }
public int SelectedCategory{get;set;}
public Item Item { get; set; }
public int SelectedDonor{get;set;}
public IEnumerable<Donor> Donors { get; set; }
}
@using (Html.BeginForm("Create", "Item"))
{
<div class="form-group">
@Html.LabelFor(m => m.Item.Donor)
@Html.DropDownListFor(m => m.SelectedDonor, new SelectList(Model.Donors, "Id", "FullName"), "Who donated this item?", new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Item.Category)
@Html.DropDownListFor(m => m.SelectedCategory, new SelectList(Model.Categories, "Id", "Name"), "Select Item Category", new { @class = "form-control" })
</div>
}
公共类NewItemViewModel
{
公共IEnumerable类别{get;set;}
public int SelectedCategory{get;set;}
公共项项{get;set;}
public int SelectedDonor{get;set;}
公共IEnumerable施主{get;set;}
}
@使用(Html.BeginForm(“创建”、“项目”))
{
@LabelFor(m=>m.Item.Donor)
@Html.DropDownListFor(m=>m.SelectedDonor,new SelectList(Model.genders,“Id”,“FullName”),“谁捐赠了这个项目?”,new{@class=“form control”})
@LabelFor(m=>m.Item.Category)
@DropDownListFor(m=>m.SelectedCategory,new SelectList(Model.Categories,“Id”,“Name”),“Select Item Category”,new{@class=“form control”})
}
在NewItemViewModel
中,您尚未创建用于保存下拉列表中所选值的属性谢谢您的回复。不幸的是,这对我仍然不起作用。
@using (Html.BeginForm("Create", "Item"))
{
<div class="form-group">
@Html.LabelFor(m => m.Item.Donor)
@Html.DropDownListFor(m => m.Item.Donor, new SelectList(Model.Donors, "Id", "FullName"), "Who donated this item?", new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Item.Category)
@Html.DropDownListFor(m => m.Item.Category, new SelectList(Model.Categories, "Id", "Name"), "Select Item Category", new { @class = "form-control" })
</div>
}
public class NewItemViewModel
{
public IEnumerable<Category> Categories { get; set; }
public int SelectedCategory{get;set;}
public Item Item { get; set; }
public int SelectedDonor{get;set;}
public IEnumerable<Donor> Donors { get; set; }
}
@using (Html.BeginForm("Create", "Item"))
{
<div class="form-group">
@Html.LabelFor(m => m.Item.Donor)
@Html.DropDownListFor(m => m.SelectedDonor, new SelectList(Model.Donors, "Id", "FullName"), "Who donated this item?", new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Item.Category)
@Html.DropDownListFor(m => m.SelectedCategory, new SelectList(Model.Categories, "Id", "Name"), "Select Item Category", new { @class = "form-control" })
</div>
}