在 Razor Pages/MVC asp 应用程序中将两个模型链接在一起



所以。我正在一个基于 Razor 的网站工作,该网站充当业务经理。

我正在尝试将地址链接到创建页面中的打印机。

打印机型号如下所示: 公共类打印机 { public int ID { get; set; }

[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; }
}

地址模型如下所示: 公共类地址 { public int ID { get; set; }

[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();
}

Create.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-for属性指定的属性来实现。

https://www.learnrazorpages.com/razor-pages/forms/select-lists#setting-selected-items1

1 披露 - 我拥有学习剃须刀页面网站

相关内容

  • 没有找到相关文章