簡體   English   中英

POST 后 Net Core 模型綁定失敗

[英]Net Core Model Binding Failure after POST

POST 后使用模型數據時遇到問題。 我已經包含了在 POST 之后調用的方法的相關代碼、執行 POST 的 .cshtml 代碼和 ViewModel。 請注意,雖然我沒有在這里包含它,但我已經嘗試使用 [FromForm]: public string UpdateSpoiler([FromForm] SpoilerAnalysisViewModel temp)

默認路由已就位。 首次生成 .cshtml 頁面時,它確實定義了正確的集名稱。 POST 后,所有數據為空。

任何幫助將不勝感激。

查看模型類:

public class SpoilerAnalysisViewModel
    {
        public string SetName = string.Empty;

        // Colors
        public bool White = true;
        public bool Blue = true;
        public bool Black = true;
        public bool Red = true;
        public bool Green = true;
        public bool Colorless = true;

        // CMC range
        public byte minCMC = 0;
        public byte maxCMC = 15;

        // Card types
        public bool Creature = true;
        public bool Instant = true;
        public bool Sorcery = true;
        public bool Enchantment = true;
        public bool Artifact = true;
        public bool Land = true;

        // Rarities
        public bool Limited = true;
        public bool Mythics = true;
        public bool Rares = true;
        public bool Uncommons = true;
        public bool Commons = true;

    }

.cshtml 文件:

@model SpoilerAnalysisViewModel;

<!DOCTYPE html>

<body>
@using (Html.BeginForm("RefreshSpoiler", "SpoilerAnalysis", FormMethod.Post))
{
    <table cellpadding="0" cellspacing="0">
        <tr>
            <td>
                @Html.CheckBoxFor(m => m.White) White
            </td>
        </tr>
        <tr>
            <td>
                @Html.TextBoxFor(m => m.minCMC) Minimum CMC
            </td>
        </tr>
        <tr>
            <td>
                @Html.TextBoxFor(m => m.SetName) Current Set
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>
}

POST 后調用的方法

    [HttpPost]
    public string RefreshSpoiler(SpoilerAnalysisViewModel temp)
    {
        if (temp == null) return "model is null";
        return "set: " + temp.SetName;
    }

您的模型只是Set它沒有Get的值。 所以如果你想設置值,你需要以另一種方式設置它:

假設你的模型是

public class SpoilerAnalysisViewModel
{
 public public bool White{ get; set; }
 ....
}

然后你可以設置值:

 SpoilerAnalysisViewModel model = new SpoilerAnalysisViewModel()
 {
   White= True;
   ....
 };

暫無
暫無

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

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