Код: Выделить всё
@page
@model RogersPizza.Pages.OrderModel
@{
Layout = "Shared/_Layout.cshtml";
}
@section title {
Order
}
@section scripts {
}
@section siteMenu {
[url=Index]Rogers Pizza[/url]
[url=Menu]Menu[/url]
Order
[url=About]About[/url]
[url=Contact]Contact[/url]
[url=Employees]Employees[/url]
}
@section body {
Choose your pizza
@foreach(var item in Model?.Pizzas)
{
@item.Name
}
}
< /code>
Вот скрипт React внутри формы: < /p>
import React from 'react';
import { useState, useEffect } from 'react';
import { createRoot } from 'react-dom/client';
export default function PaymentOptions() {
const [paymentOption, setPaymentOption] = useState("cash");
const [giftCard, setGiftCard] = useState("");
useEffect(() => {
const form = document.querySelector('form');
const syncHiddenInputs = () => {
const paymentInput = document.getElementById('selectedPaymentOption');
const giftCardInput = document.getElementById('giftCardNumber');
if (paymentInput) {
paymentInput.value = paymentOption;
}
if (giftCardInput) {
// if(paymentOption === "cash")
// {
// giftCardInput.value = "9999999999999999";
// }
// else if(paymentOption === "giftCard")
// {
// giftCardInput.value = giftCard || "";
// }
giftCardInput.value = paymentOption === "cash" ? "9999999999999999" : giftCard;
}
};
form?.addEventListener('submit', syncHiddenInputs);
return () => form?.removeEventListener('submit', syncHiddenInputs);
}, [paymentOption, giftCard]);
const handlePaymentMethodChange = (e) => {
setPaymentOption(e.target.value);
};
const handleGiftCardChange = (e) => {
setGiftCard(e.target.value);
};
return (
Please select a payment method
Cash
Gift Card
{paymentOption === "cash" &&
Please have your cash ready when receiving your order.
}
{paymentOption === "giftCard" &&
Please enter your gift card number below.
}
);
}
const domNode = document.getElementById('payment-options');
if (domNode) {
const root = createRoot(domNode);
root.render(
);
}
< /code>
Вот страница модели: < /p>
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using RogersPizza.Models;
namespace RogersPizza.Pages
{
public class OrderModel : PageModel
{
private readonly RogersPizza.Data.StoreContext _context;
private readonly ILogger _logger;
[BindProperty] public Order? Order { get; set; }
public OrderModel(RogersPizza.Data.StoreContext context, ILogger logger)
{
_context = context;
_logger = logger;
}
public IList? Pizzas { get; set; }
public async Task OnGetAsync()
{
Pizzas = await _context.Pizzas.ToListAsync();
}
public IActionResult OnPost()
{
_logger.LogInformation("Received order: Pizza={Pizza}, Payment={PaymentOption}, GiftCard={GiftCardNumber}",
Order?.Pizza, Order?.PaymentOption, Order?.GiftCardNumber);
return RedirectToPage("/Index");
}
// private bool ValidateOrder(Order order)
// {
// return false;
// }
}
}
< /code>
Вот модель заказа, используемая при привязке формы: < /p>
namespace RogersPizza.Models
{
public class Order
{
public int? ID { get; set; }
public required string Pizza { get; set; }
public required string PaymentOption { get; set; }
public string? GiftCardNumber { get; set; }
}
}
< /code>
В настоящее время моя форма формы не выполняется при отправке формы на странице представления, и я получаю страницу сообщения об ошибке 400 при использовании Chrome с следующим сообщением в консоли: < /p>
Не удалось загрузить ресурс: сервер отвечал статусом 400 () < /p>
< /> < /blockquote>
paymentOptions=giftCard&Order.ID=999
Подробнее здесь: https://stackoverflow.com/questions/797 ... s-http-400