下午所有,即时通讯新的mvc和实体框架。但是我提到我的模型时有一个问题。EntityFramework的下拉列表不显示选项
我已经合并这两个表到一个模型命名Marie_Testing.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EDT_Test.Models.ViewModels
{
public partial class Marie_Testing
{
[Display(Name = "Client Ref:")]
public int id { get; set; }
[Display(Name = "Created By:")]
public string CreatedBy { get; set; }
public List<Title> allTitles { get; set; }
[Required]
[Display(Name = "Surname:")]
public string Surname { get; set; }
[Display(Name = "Additional Surname:")]
public string Surname2 { get; set; }
[Required]
[Display(Name = "Forename:")]
public string Forename1 { get; set; }
[Display(Name = "Additional Forename:")]
public string Forename2 { get; set; }
我有一个创建。使用该Marie_Testing模型CSHTML页...
@model EDT_Test.Models.ViewModels.Marie_Testing
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@*@Html.HiddenFor(model => model.allClientData)*@
<div class="form-horizontal">
<h4>Marie_Test</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CreatedBy, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreatedBy, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", style = "width:200px;background:E1EBF9;", @Value = "LazzM" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.allTitles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select id="titleid" name="titlename" class="form-control">
@foreach (var item in Model.allTitles)
{
<option>@item.id</option>
}
</select>
</div>
</div>
我有一个应该为LI HomeController.cs在创建页面时从数据库的下拉列表中标题。这似乎并不奏效。以下是“创建”操作结果的代码。
//required to get Client data from the Clients: Dbset
[HttpGet]
public ActionResult Create()
{
////set the defaults (dropdown) of the page for the create of of a new record.
Marie_Testing vmPopulateData = new Marie_Testing();
List<Title> titles = (from t in db.Titles select t).ToList();
vmPopulateData.allTitles = titles;
return View(vmPopulateData);
}
//required to post ClientML table, vreate a parameter of client
[HttpPost]
public ActionResult Create(ClientRecord client)
{
//validate the model and save to employee table.
if (ModelState.IsValid)
{
//this is the db set we are referencing
db.ClientRecords.Add(client);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(client);
}
我可以跑不过当前的代码,并成功地将数据输入到所需的数据库,但是标题字段未填充,因为我可以看到这个调试和步进通过创建代码时。
我不知道为什么标题下拉列表不被填充,因为我可以看到模型在Create.cshtml引用,我可以从剃刀代码中的模型选择这些属性。
我看过一些类似于我的问题的帖子,但我找不到解决问题的方法。
如果有人有任何建议或指点,请你让我知道。
的''的select' html元素的属性name'必须是相同的视图模型属性'allTitles'。所以基本上:'
您的视图模型中似乎没有标题值?我只能看到标题列表,但没有具体声明来捕获实际标题 –
为什么'ClientRecord client'是'ClientRecord'类型'Create' ActionResult的参数?对于它的工作,参数必须是视图模型类型的,所以你的函数应该是这个样子:'公众的ActionResult创建(Marie_Testing客户端)' – Kapobajza