简体   繁体   English

Asp.Net Core Razor Pages DropDownList 选定的项目值

[英]Asp.Net Core Razor Pages DropDownList selected item values

I'm new to Asp.Net Core Razor Pages.我是 Asp.Net Core Razor Pages 的新手。 And this is my first question on Stack Overflow.这是我关于 Stack Overflow 的第一个问题。 When a client is selected from a drop down list on the create jobs page I want to receive the client details from the clients page where the client is registered on and then display the cell phone number in the corresponding form field on the create a job page.当从创建工作页面的下拉列表中选择客户时,我想从注册客户的客户页面接收客户详细信息,然后在创建工作页面的相应表单字段中显示手机号码.

Thank you in advance.先感谢您。

Client Model:客户端型号:

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

Job Model:工作模式:

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

On the job create page.cshtml the client must be selected from a dropdownlist and then the clients' Cellphone number must be displayed in the Cellphone number form field.在作业创建 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>

Create job page.cshtml.cs:创建作业页面.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();
    }
}

Populate drop down list 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 (and presumably Plumber.ID as well) are typed as int . Client.ID (也可能是Plumber.ID )被输入为int However, on your Job , you've made the foreign key property typed as string .但是,在您的Job ,您已将外键属性输入为string Additionally, your dropdown lists are using the FullName property as both the text and the value of the select options.此外,您的下拉列表使用FullName属性作为选择选项的文本和值。

Change your Job foreign keys ( ClientID and PlumberID ) to be int to match the actual type of the PK, and then set the value of your options to the ID property:将您的Job外键( ClientIDPlumberID )更改为int以匹配 PK 的实际类型,然后将您的选项的值设置为ID属性:

ClientNameSL = new SelectList(clientQuery.AsNoTracking(),
        "ID", "FullName", selectedClient);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM