- Home
- Forum
- ASP.NET Core - EJ 2
- BindProperty on Razor Pages
BindProperty on Razor Pages
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
...
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>
<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.
{
if (!ModelState.IsValid)
{
return Page();
}
var x = AccessType; //AccessType is always null, even if something is selected in the dropdown.
...
return Page();
}
return Page();
}
Thanks,
Mike
SIGN IN To post a reply.
3 Replies
PO
Prince Oliver
Syncfusion Team
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
ML
Michael Lambert
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
PO
Prince Oliver
Syncfusion Team
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
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
ML Michael Lambert
- Mar 6, 2019 10:16 PM UTC
- Mar 8, 2019 04:02 AM UTC