Skip to content

Instantly share code, notes, and snippets.

@andy-williams
Created March 6, 2019 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy-williams/6bb8e5cd43623f6355be8769e86f6c91 to your computer and use it in GitHub Desktop.
Save andy-williams/6bb8e5cd43623f6355be8769e86f6c91 to your computer and use it in GitHub Desktop.
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