如何建模绑定剃须刀页面模型的复杂类型属性?

时间:2020-01-29 19:44:48

标签: c# asp.net-core-mvc asp.net-core-2.2

我有一个剃须刀页面,它接收一个PageModel后代。 对于仅显示页面(仪表板,报告等),它的作用就像一个超级按钮。我使用基本的PageModel类来使所有页面都能反映我的hosting.json配置,从而使应用程序内部的链接动态化。

因此层次结构为:PageModel-> BasePageModel-> ConfigTransmissorFragModel

现在我有了这个SendingConfiguration模型(我将在较低层中对其进行处理):

import random


l = [15, 30, 45]
N = 30
result = [random.choice(l) for i in range(N)]

# outputs: [15, 30, 15, 45, 15, 15, 30, 15, 30, 45, 15, 45, 30, 15, 30, 15, 15, 45, 30, 45, 30, 45, 45, 15, 15, 45, 30, 45, 45, 45]

PageModel如下:

public class SendingConfiguration
{
    // I will edit this one
    [DisplayName("Send it to production server?")]
    public bool ProductionServer { get; set; }
}

模型是在控制器中生成的:

// BasePageModel will process the urls of the site.
public class ConfigTransmissorFragModel : BasePageModel
{

    public SendingConfiguration SendConfig { get; set; }

    private void UpdateSendingConfiguration()
    {
        var Config = InfoExibicao.ConfigAtiva;
        ConfigEnvio.ProductionServer = Config.ProductionServer;
    }

    private InfoConfiguration _InfoExibition;
    public InfoConfiguration InfoExibition
    {
        get { return _InfoExibition; }
        set
        {
            _InfoExibition = value;

        }
    }

    public ConfigTransmissorFragModel() : base(null)
    {
        SendConfig = new SendingConfiguration();   
    }

    public ConfigTransmissorFragModel(
        ConfigDashboard PConfigDash,
        InfoConfiguration PInfoConfig
        ) : base(PConfigDash)
    {
        ConfigEnvio = new SendingConfiguration();
        this.InfoExibition = PInfoConfig;
        UpdateSendingConfiguration();
    }
}

在剃须刀页面中,我有以下标题:

var Fch = FacadeInfoConfig;
var InfoConfig = Fch.ObterInfoConfiguracao(IdTransmissor);
var CfgDash = ConfigDash;

var Modelo = new ConfigTransmissorFragModel(CfgDash, InfoConfig);
Modelo.UrlServer = ConfigHost.WebServiceUrl;

表格:


@page
@using ESenderWebService.ModeloPagina
@using Microsoft.AspNetCore.Mvc.RazorPages;
@using Negocio.Integracao.Configuracao;
@model ESenderWebService.ModeloPagina.ConfigTransmissorFragModel
@{
    //ViewData["Title"] = "Configuracao de Transmissor para envio";
    Layout = "~/Pages/Shared/_Layout_Dashboard.cshtml";
}

我的问题是:如何在我的帖子处理程序上接收SendingConfiguration?

    <form autocomplete="off" asp-controller="InfoConfig" asp-action="SalvarConfig" method="post">
        <div class="form-group">
            <fieldset>
                <legend style="width: auto; margin-bottom: auto;"> Configurações </legend>
                            <div>
                                @Html.CheckBoxFor(m => m.SendConfig.ProductionServer)
                                @Html.LabelFor(m => m.SendConfig.ProductionServer)
                            </div>
                            <div>
                                <button class="btn btn-primary " name="submit" type="submit">Save</button>
                            </div>

            </fieldset>
        </div>
    </form>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我不确定第一个HttpPost语法为什么会爆炸,因为它在我的示例项目中工作正常。

enter image description here