下午所有,我是mvc和实体框架的新手。但我对我的模型提出了一个问题。
我将这两个表组合成一个名为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模型的Create.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>
我有一个HomeController.cs,它应该在创建页面时列出数据库下拉列表中的标题。这似乎不起作用。以下是&#39;创建&#39;的代码。行动结果。
//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中引用了模型,我可以在剃刀代码中从模型中选择这些属性。
我见过一些类似于我的问题的类似帖子,但我找不到解决问题的方法。
如果有人有任何建议或指示,请告诉我。
答案 0 :(得分:0)
请确保在选项标记中添加“value”属性。 我还假设你应该将item.Title1显示为文本,而不是item.id(如果我错了,请更正我)
请添加
值= “@ item.id”
到选项标签,否则它不会发布选定的值。
<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 value="@item.id">@item.Title1</option>
}
</select>
</div>
</div>
此外,如果您的选择标记为空且没有任何值,我建议您检查您的表是否有一些记录。