ahmed salah

ahmed salah

  • 931
  • 547
  • 64.4k

How to fill data I get it on pge model on drop down list on view ?

May 4 2023 12:43 AM

I worked on asp.net mvc razor pages . I face issue I can't pass data from page model to view

what i try

1- I create view model as

public class AddUserViewModel
{

    public int SelectedBranchId { get; set; }
    public List<Branch> Branches { get; set; }
}
2-Branch Model

public class Branch
{
    [Key]
    public string iBranchCode { get; set; }
    public string vBranchDesc { get; set; }
}
3 - on razor pages AddUser.cshtml.cs I get 81 rows on userModel.Branches

public class AddUserModel : PageModel
{
    [BindProperty]
    public AddUserViewModel userModel { get; set; }
    private readonly ADCContext _db;
    public AddUserModel(ADCContext db)
    {
        _db = db;
        userModel = new AddUserViewModel();
    }
    public void OnGet()
    {
        userModel.Branches= _db.Branch.ToList();// from debug get 81 rows
       //userModel.Branches have 81 rows
    }
}
4 - on view AddUser.cshtml (main issue )

How to access userModel.Branches Branch List from page model to view

meaning How to fill (81 rows) on drop down list on view from data I received on page model

@page "/AddUser"
@model UC.ADC.Host.Pages.Users.AddUserModel

<div class="form-group row">
    <label for="branch-select" class="col-sm-1 col-form-label" style="font-size:15px;font-family: 'Open Sans', sans-serif;font-weight: bold;">Branch</label>
    <div class="col-sm-3">
        <select id="branch-select" name="SelectedBranchId"  class="form-control" style=" margin-left:10px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;">
            <option value="">-- Select Branch --</option>

            @foreach (var branch in Model.????)
            {
                <option value="@branch.iBranchCode">@branch.vBranchDesc</option>
            }
        </select>
    </div>
</div>
asp.net-mvcasp.net-core.net-corerazor-pages

 


Answers (1)