簡體   English   中英

獲取ASP.NET Core Pass表單值

[英]ASP.NET Core Pass form values on get

這是我的CS頁面:

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace CoreRazor2.Pages
{
  public class IndexModel : PageModel
  {
    [BindProperty]
    public int result { get; set; }
    public void OnGet()
    {
      if (Request.Form["+"] == true)
      {
          result = Int32.Parse(Request.Form["first"]) + Int32.Parse(Request.Form["second"]);
      }
      else if (Request.Form["-"] == true)
      {
        // code for function 2
      }
      else if (Request.Form["*"] == true)
      {

      }
      else if (Request.Form["/"] == true)
      {

      }
    }
  }
}

和我的cshtml頁面:

@page
@model IndexModel
@{
    ViewData["Title"] = "Calculator";
}

<form method="GET">
<label>First Value: </label>
<input name="first"/>
<br/>
<br/>
<label>Second Value: </label>
<input name="second"/>
<br/>
<br/>
<input type="submit" name="+" value="+"/>
<input type="submit" name="-" value="-"/>
<input type="submit" name="*" value="*"/>
<input type="submit" name="/" value="/"/>
</form>
@Model.result

我試圖弄清楚哪個提交被單擊,然后根據單擊的按鈕執行不同的操作。 您如何通過獲取請求來執行此操作。 代碼給我一個錯誤-第一次Request.Form ['+']檢查時出現'InvalidOperationException'。 如何正確執行此操作?

如果使用的是MVC5,則需要通過控制器進行此操作。 這可能適用於其他版本,但我尚未測試(並且不確定您實際使用哪種技術)

為所有提交按鈕指定相同的名稱,這是您將作為參數獲取的值

// Update your HTML as follow
<input type = "submit" name = "operationType" value = "+"/>
<input type = "submit" name = "operationType" value = "-"/>
<input type = "submit" name = "operationType" value = "*"/>
<input type = "submit" name = "operationType" value = "/"/>

並將您的控制器更改為具有“ operationType”參數,如下所示

public ActionResult MyAction(string operationType)
{
    int result = 0;
    switch(operationType)
    {
        case "+":
            result = Int32.Parse(Request.Form["first"]) + Int32.Parse(Request.Form["second"]);
            break;
        case "-":
            break;
        case "/":
            break;
        case "*":
            break;
    }
}

您可以使用asp-page-handler https://docs.microsoft.com/zh-cn/aspnet/core/mvc/razor-pages/?tabs=visual-studio

<input type="submit" asp-page-handler="JoinList" value="Join" />
<input type="submit" asp-page-handler="JoinListUC" value="JOIN UC" />

 public Customer Customer { get; set; }

        public async Task<IActionResult> OnPostJoinListAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _db.Customers.Add(Customer);
            await _db.SaveChangesAsync();
            return RedirectToPage("/Index");
        }

        public async Task<IActionResult> OnPostJoinListUCAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            Customer.Name = Customer.Name?.ToUpper();
            return await OnPostJoinListAsync();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM