【发布时间】:2020-09-15 21:17:04
【问题描述】:
所以。我正在开发一个基于 Razor 的网站,担任业务经理。
我正在尝试将地址链接到创建页面中的打印机。
打印机型号如下所示: 公共类打印机 { 公共 int ID { 获取;放; }
[StringLength(50)]
[Display(Name = "Machine ID")]
public string MachineID { get; set; }
[Display(Name = "Log ID")]
public int Log_ID { get; set; }
[Display(Name = "Lease ID")]
public int Lease_ID { get; set; }
[Display(Name = "Serial Number")]
[StringLength(50)]
public string Serial_Number { get; set; }
[StringLength(10)]
public string Solution { get; set; }
[StringLength(50)]
[Display(Name = "Additional Info")]
public string Additional_Info { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Installation Date")]
public DateTime InstallationDate { get; set; }
[Display(Name = "Mono Click")]
public float Mono_Click { get; set; }
[Display(Name = "Colour Click")]
public float Colour_Click { get; set; }
public int Minimum { get; set; }
[Display(Name = "Parts Warranty")]
public int Parts_Warranty { get; set; }
[Display(Name = "IT Support")]
public int IT_Support { get; set; }
[StringLength(50)]
[Display(Name = "Contract Type")]
public string Contract_Type { get; set; }
public Company_Site Site { get; set; }
public Printer_Model Model { get; set; }
public Address Previous_Address { get; set; }
}
地址模型如下所示: 公开课地址 { 公共 int ID { 获取;放; }
[StringLength(50)]
[Display(Name = "Address")]
public string Address_1 { get; set; }
[Display(Name = "Address")]
[StringLength(50)]
public string Address_2 { get; set; }
[StringLength(50)]
[Display(Name = "City")]
public string Town_City { get; set; }
[StringLength(50)]
public string County { get; set; }
[Display(Name = "Post Code")]
[StringLength(10)]
public string Post_Code { get; set; }
}
为了尝试将它们链接在一起,我在我的 Create.cshtml.cs 文件中有这个:
public List<SelectListItem> PrevAddress { get; set; }
public IActionResult OnGet()
{
PrevAddress = _context.Address.Select(a => new SelectListItem
{
Value = a.ID.ToString(),
Text = a.Address_1 + " " + a.Town_City + " " + a.Post_Code
}).ToList();
return Page();
}
创建.cshtml:
<div class="form-group">
<label asp-for="Printer.Previous_Address.ID" class="control-label"></label>
<select asp-for="Printer.Previous_Address.ID" name="PrevAddress" asp-items="Model.PrevAddress" class="form-control"><option value=""></option></select>
<span asp-validation-for="Printer.Previous_Address.ID" class="text-danger"></span>
</div>
但是,当我通过我的表单运行它时,即使我从下拉框中明确选择了我的地址之一,也不会将值发送到数据库中的外键字段。对此的任何帮助将不胜感激。
【问题讨论】:
标签: asp.net asp.net-mvc razor-pages