C# aspnetusers表中的ViewBag下拉列表未在post时保存到数据库
我有一个从“ASP.NET Identity”、“aspnetusers”表中名为“FullName”的自定义属性创建的下拉列表中的项目列表。我的目标是从下拉列表中选择名称,并将全名和Id保存在我的“应用程序”表中,创建一对多关系。下拉列表正在填充这些名称,但没有将它们保存到我的应用程序表中 型号:C# aspnetusers表中的ViewBag下拉列表未在post时保存到数据库,c#,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,我有一个从“ASP.NET Identity”、“aspnetusers”表中名为“FullName”的自定义属性创建的下拉列表中的项目列表。我的目标是从下拉列表中选择名称,并将全名和Id保存在我的“应用程序”表中,创建一对多关系。下拉列表正在填充这些名称,但没有将它们保存到我的应用程序表中 型号: public class HomeController : Controller { private ApplicationDbContext db = new ApplicationDbC
public class HomeController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public class Application
{
public int ApplicationId { get; set; }
[Required]
[DisplayName("First Name")]
public string FirstName { get; set; }
[Required]
[DisplayName("Last Name")]
public string LastName { get; set; }
*****Asp.net identity application user class that I have a custom full name property in*****
public virtual ApplicationUser ApplicationUser { get; set; }
}
控制器:
public ActionResult Index()
{
ViewBag.LoanOfficer = new SelectList(db.Users,"Id","FullName");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "Id,FullName,FirstName,LastName")] Application application)
{
if (ModelState.IsValid)
{
ViewBag.LoanOfficer = new SelectList(db.Users, "Id", "FullName", application.ApplicationUser);
db.Applications.Add(application);
db.SaveChanges();
return RedirectToAction("Thanks");
}
return View(application);
}
视图:
@Html.DropDownList("LoanOfficer", "Select Loan Officer")
您的模型没有名为
LoanOfficer
的属性,因此无法对其进行绑定。如果检查Request.Form
,您将看到所选用户的ID
值,因此您需要在保存它之前手动将其添加到应用程序中。但是您的代码有很多问题,包括使用@Html.DropDownList()
,毫无意义地使用[Bind(Include=“…”)]
(您列出了所有3个属性,但它们都是默认绑定的,并且您包含了“FullName”,它甚至不是模型的属性),如果模型有效,则重新创建SelectList
(这是毫无意义的,因为您会立即重定向,但如果返回视图,则不会创建它-这意味着dropdownlist中不会显示任何内容)
创建视图模型以表示要显示和编辑的内容
public class ApplicationVM
{
[Required]
[DisplayName("First Name")]
public string FirstName { get; set; }
[Required]
[DisplayName("Last Name")]
public string LastName { get; set; }
[Required]
[DisplayName("Loan officer")]
public int? LoanOfficer { get; set; }
public SelectList LoanOfficerList { get; set; }
}
控制器
public ActionResult Create()
{
ApplicationVM model = new ApplicationVM();
model.LoanOfficerList = new SelectList(db.Users, "Id", "FullName");
return View();
}
[HttpPost]
public ActionResult Create(ApplicationVM model)
{
if(!ModelState.IsValid)
{
model.LoanOfficerList = new SelectList(db.Users, "Id", "FullName");
return View(model);
}
// Initialize new Application, set properties from the view model, save and redirect
}
看法
@model ApplicationVM
@使用(Html.BeginForm())
{
....
//FirstName和LastName属性的控件
@Html.LabelFor(m=>m.LoanOfficer)
@Html.DropDownListFor(m=>m.LoanOfficer,Model.LoanOfficerList,“--请选择--”)
@Html.ValidationMessageFor(m=>m.LoanOfficer)
}
您似乎只绑定到“Id、FullName、FirstName、LastName”,如何从“ApplicationUser”的视图中获取所选值,因为您没有看到绑定到存储此值的任何对象。在哪里声明了db
?我的视图中只有一个@HtmlDropDownList。在上面添加了它。我在索引操作方法的正上方声明了它。我不确定我是否理解你的意思。介意举个例子吗?“Id”和“FullName”来自aspnetusers表。
@model ApplicationVM
@using(Html.BeginForm())
{
....
// controls for FirstName and LastName properties
@Html.LabelFor(m => m.LoanOfficer)
@Html.DropDownListFor(m => m.LoanOfficer, Model.LoanOfficerList, "--Please select--")
@Html.ValidationMessageFor(m => m.LoanOfficer)
<input type="submit" value="Create" />
}