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

问题描述 投票:0回答:1

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

所以层次结构是:PageModel-> BasePageModel-> ConfigTransmissorFragModel

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

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

PageModel如下:

// 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";
}

表格:


    <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>

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

    // This one explodes complaining that HttpContext is missing  
    [HttpPost()]
    public IActionResult SalvarConfig(ConfigTransmissorFragModel PModel)


    // This one never reflects what I mark in the form
    // It always returns false in PModel.ProductionServer
    [HttpPost()]
    public IActionResult SalvarConfig([FromForm] SendingConfiguration PModel)

我做错了什么?

c# asp.net-core-mvc asp.net-core-2.2
1个回答
0
投票

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.