Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // *** This goes to Program.cs - use static file middleware ***
- app.UseStaticFiles();
- // *** Create wwwroot folder in project and a new index.html file in it. ***
- // *** This goes to index.html ***
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>Form example - receipt</title>
- </head>
- <body>
- <form method="post" action="https://localhost:7246/api/Receipt/form">
- <input type="hidden" name="ReceiptId" value="12" />
- <select name="ReceiptTitle">
- <option value="">(select subscription)</option>
- <option value="0">Monthly Subscription</option>
- <option value="1">Monthly Subscription - Family</option>
- <option value="2">Quarterly Subscription</option>
- <option value="3">Annual Subscription</option>
- </select>
- <br />
- <input type="text" name="ReceiptTotal" placeholder="Total" />
- <br />
- <input type="submit" value="Update data" />
- </form>
- </body>
- </html>
- // *** Add the model for the form handler ***
- public class ReceiptFormData
- {
- public int ReceiptId { get; set; }
- public int? ReceiptTitle { get; set; }
- public decimal? ReceiptTotal { get; set; }
- }
- // *** Add the form handler to the controller ***
- [HttpPost("[action]")]
- public ActionResult<Receipt> Form([FromForm] ReceiptFormData data)
- {
- var titles = new string[] { "Monthly Subscription", "Monthly Subscription - Family", "Quarterly Subscription", "Annual Subscription" };
- var retVal = _receipts.FirstOrDefault(x => x.Id == data.ReceiptId);
- if (retVal == null)
- return NotFound();
- if(data.ReceiptTitle.HasValue)
- retVal.Title = titles[data.ReceiptTitle.Value];
- if (data.ReceiptTotal.HasValue)
- retVal.Total = data.ReceiptTotal.Value;
- return Redirect("/index.html");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement