We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

BindProperty on Razor Pages

Thread ID:

Created:

Updated:

Platform:

Replies:

143161 Mar 6,2019 10:16 PM UTC Mar 8,2019 04:02 AM UTC ASP.NET Core - EJ 2 3
loading
Tags: Dropdown List
Michael Lambert
Asked On March 6, 2019 10:16 PM UTC

How do you bind the text or value property to a property in the @Model:

In the Model:
    public class AdminModel : PageModel
    {
...
        [BindProperty]
        public string AccessType { get; set; }
        public string[] AccessTypes = new string[] { "Officer", "Supervisor", "TSAdmin", "Admin"};  //This binding work fine
...

Markup
        <div class="row">
            <ejs-dropdownlist id="ddAccessType" width="200" dataSource="@Model.AccessTypes"  text="@Model.AccessType" placeholder="Select Access Type" change="accessChange"  popupHeight="220px">
            </ejs-dropdownlist>
        </div>

Post Method.

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            var x = AccessType;   //AccessType is always null, even if something is selected in the dropdown.
...
            return Page();
        }

Thanks,
Mike

Prince Oliver [Syncfusion]
Replied On March 7, 2019 08:57 AM UTC

Hi Michael, 

Thank you for contacting Syncfusion support. 

Based on the shared code snippet, we suspect that the cause of the problem is that model data is accessible in the server side based on name attribute of the dropdown element. Hence, we suggest you set the name attribute in DropDownList element. Kindly refer to the following code snippet. 

[.cshtml] 
<form id="form-element" method="post"> 
    <ejs-dropdownlist id="ddAccessType" width="200" name="AccessType" dataSource="Model.AccessTypes" value="Model.AccessType" placeholder="Select Access Type" popupHeight="220px"> 
    </ejs-dropdownlist> 
    <div class="form-group"> 
        <div class="col-md-10"> 
            <button type="submit" class="e-control e-btn">Submit</button> 
        </div> 
    </div> 
</form> 

[cs] 
public class IndexModel : PageModel 
{ 
    public string AccessType { get; set; } 
    public string[] AccessTypes { get; set; } 
    public void OnGet() 
    { 
             
        AccessTypes = new ListItems().getListItems(); 
        AccessType = "TSAdmin"; 
 
    } 
    [HttpPost] 
    public async Task<IActionResult> OnPostAsync() 
    { 
        if (!ModelState.IsValid) 
        { 
            return Page(); 
        } 
        AccessTypes = new ListItems().getListItems(); 
        AccessType =  Request.Form["AccessType"]; 
        return Page(); 
    } 
} 
public class DataModel 
{ 
    public string AccessType { get; set; } 
    public string[] AccessTypes { get; set; } 
 
} 

We have attached the sample for your reference, please find the sample at the following location: http://www.syncfusion.com/downloads/support/forum/143161/ze/dropdownAsp.NetCore_Razor-1919341082.zip  

Please let us know if you need any further assistance on this. 

Regards, 
Prince 


Michael Lambert
Replied On March 7, 2019 05:35 PM UTC

Hi Prince,
That worked, though as an FYI you don't need to do:  AccessType =  Request.Form["AccessType"];, The 'AccessType' property is automatically populated on return, which is nice.

Mike

Prince Oliver [Syncfusion]
Replied On March 8, 2019 04:02 AM UTC

Hi Michael, 
 
Thank you for your update. We are glad that the issue is resolved in your end. Please let us know if you need any further assistance on this. 
 
Regards, 
Prince 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon