Это мой файл React.Js:
Код: Выделить всё
const defaultImgPath = '/assets/ImgStarter.jpg';
const initialFieldValues = {
productName: 'Fried spring rolls',
productPrice: 50000,
productStatus: 1,
productDescription: 'A short description',
imagePath: defaultImgPath,
imgName: '',
ProductImg: null
}
const ApiUploadImg = () => {
const [input, setInput] = useState(initialFieldValues)
const handleAddProduct = (e) => {
e.preventDefault();
axios.post('http://localhost:5273/api/product', input)
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error.response);
})
return (
[img]{input.imagePath}
/>
Submit
)
}
Код: Выделить всё
const Input = ({ setInput, input, labelname, name, inputType, onChange=null}) => {
const [value, setValue] = useState('');
const onChangeHanlder = (e) => {
setInput({...input, [name]: e.target.value})
}
var id = {name};
return (
{labelname}:
)
}
Код: Выделить всё
[HttpPost]
public async Task Create([FromBody] CreateProductRequestDTO requestDTO)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var productModel = requestDTO.ToProductsFromCreateDTO();
await _productRepo.CreateAsync(productModel);
return CreatedAtAction(nameof(GetById), new {id = productModel.Id}, productModel.ToProductDTO());
}
Код: Выделить всё
public class CreateProductRequestDTO
{
//Solution (1)
// public string? ProductName {get; set; } = string.Empty;
// public int ProductPrice {get; set; }
// public bool ProductStatus {get; set; }
// public string? ProductDescription {get; set; } = string.Empty;
//Solution (2)
public string Name {get; set; } = string.Empty;
public int Price {get; set; }
public bool Status {get; set; }
public string Description {get; set; } = string.Empty;
}
Может кто-нибудь объяснить мне, что происходит?
Подробнее здесь: https://stackoverflow.com/questions/783 ... p-net-work