Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
React inline error with asp.net Model State errors
[Route("api/v1/account")]
[AllowAnonymous]
public class ApiController : Controller
{
[Route("login")]
public async Task<IActionResult>Login([FromBody] LoginRequest login)
{
if (ModelState.IsValid)
{
// ... logic
}
return BadRequest(ModelState.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
));
}
}
import * as React from 'react'
export const InlineError = ({ field, errors }) => {
if(!errors) {
return null
}
if(!errors[field])
return null
return (<div className='errors-container'>
<ul>
{errors[field].map(error => <li>{error}</li>)}
</ul>
</div>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment