[英]ASP.NET Core with Razor Pages: Return value from DropDownList
[英]Asp.Net Core Razor Pages DropDownList selected item values
我是 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>
創建作業頁面.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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.