我是Asp.Net Core Razor Pages的新手。这是我关于Stack Overflow的第一个问题。从创建作业页面上的下拉列表中选择客户端时,我希望从客户端注册的客户端页面接收客户端详细信息,然后在创建作业页面的相应表单字段中显示手机号码。
先感谢您。
客户端型号:
public class Client
{
public int ID { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
[Column("FirstName")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[DataType(DataType.PhoneNumber)]
[Display(Name = "Cellphone Number")]
public string CellNumber { get; set; }
[EmailAddress]
[Display(Name = "Email Address")]
public string Email { get; set; }
[Display(Name = "Residential Address")]
public string ResidentialAddress { get; set; }
[Display(Name = "Suburb")]
public string Suburb { get; set; }
[Display(Name = "City")]
public string City { get; set; }
[Display(Name = "Province")]
public string Province { get; set; }
[Display(Name = "Postal Code")]
public string PostalCode { get; set; }
[Display(Name = "Physical Address")]
public string PhysicalAddress
{
get
{
return ResidentialAddress + ", " + Suburb + ", " + City + ", " + Province + ", " + PostalCode;
}
}
[Display(Name = "Full Name")]
public string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
工作模式:
public class Job
{
public int ID { get; set; }
[StringLength(50, MinimumLength = 3)]
public string Title { get; set; }
[DataType(DataType.DateTime)]
public string AppointmentDate { get; set; }
[BindProperty, MaxLength(300)]
public string Description { get; set; }
public string PlumberID { get; set; }
public string ClientID { get; set; }
public Plumber Plumber { get; set; }
public Client Client { get; set; }
}
在作业创建page.cshtml时,必须从下拉列表中选择客户端,然后客户端的手机号码必须显示在手机号码表单字段中。
<div class="form-group">
<label asp-for="Job.Client" class="control-label"></label>
<select asp-for="Job.ClientID" class="form-control"
asp-items="@Model.ClientNameSL">
<option value="">-- Select Client --</option>
</select>
<span asp-validation-for="Job.ClientID" class="text-danger" />
</div>
<div class="form-group">
<label asp-for="Job.Client.CellNumber" class="control-label"></label>
<input asp-for="Job.Client.CellNumber" class="form-control" disabled />
<span asp-validation-for="Job.Client.CellNumber" class="text-danger"></span>
</div>
创建作业page.cshtml.cs:
public class CreateJobModel : DropDownPageModel
{
private readonly RealAssisst.Data.ApplicationDbContext _context;
public CreateJobModel(RealAssisst.Data.ApplicationDbContext context)
{
_context = context;
}
public IActionResult OnGet()
{
PopulatePlumbersDropDownList(_context);
PopulateClientsDropDownList(_context);
return Page();
}
[BindProperty]
public Job Job { get; set; }
public Client Client { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var emptyJob = new Job();
if (await TryUpdateModelAsync<Job>(
emptyJob,
"job",
s => s.ID, s => s.PlumberID, s => s.ClientID, s => s.Title, s => s.AppointmentDate, s => s.Description))
{
_context.Jobs.Add(emptyJob);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
PopulatePlumbersDropDownList(_context, emptyJob.PlumberID);
PopulateClientsDropDownList(_context, emptyJob.ClientID);
return Page();
}
}
填充下拉列表页面:
public class DropDownPageModel : PageModel
{
public SelectList PlumberNameSL { get; set; }
public SelectList ClientNameSL { get; set; }
public string ClientNumber { get; set; }
public void PopulatePlumbersDropDownList(ApplicationDbContext _context,
object selectedPlumber = null)
{
var plumbersQuery = from d in _context.Plumber
orderby d.FirstName
select d;
PlumberNameSL = new SelectList(plumbersQuery.AsNoTracking(),
"FullName", "FullName", selectedPlumber);
}
public void PopulateClientsDropDownList(ApplicationDbContext _context,
object selectedClient = null)
{
var clientQuery = from d in _context.Client
orderby d.FirstName
select d;
ClientNameSL = new SelectList(clientQuery.AsNoTracking(),
"FullName", "FullName", selectedClient);
}
}
Client.ID
(也可能是Plumber.ID
)被命名为int
。但是,在你的Job
上,你已经将外键属性输入为string
。此外,您的下拉列表使用FullName
属性作为选项的文本和值。
将您的Job
外键(ClientID
和PlumberID
)更改为int
以匹配PK的实际类型,然后将选项的值设置为ID
属性:
ClientNameSL = new SelectList(clientQuery.AsNoTracking(),
"ID", "FullName", selectedClient);