我如何将模型传输到Blazor服务器端的Razor组件?

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

我正在使用Blazor服务器端创建聊天室。

由于接收消息和发送消息的样式不同,我制作了一个名为MsgModel的模型>

using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorApp1
{
    public class MsgModel: ComponentBase
    {
        public string MsgText { get; set; }
    }
}

剃须刀组件ReceiveMsg.razorSendMsg.razor基于此模型。

@inherits MsgModel
<h3>ReceiveMsg</h3>@MsgText    

 @inherits MsgModel
 <h3>SendMsg</h3>@MsgText        

index.razor中,我想输入消息文本并立即显示。

@page "/"

@foreach (MsgModel _MsgModel in MsgList)
{
    if (_MsgModel.GetType() == typeof(ReceiveMsg))
    {
        <ReceiveMsg></ReceiveMsg>
    }
    else
    {
        <SendMsg></SendMsg>
    }
}
<div id="inputDiv">
    <EditForm Model="_InputMsgModel" OnValidSubmit="@SubmitText">
        <InputText @bind-Value="_InputMsgModel.MsgText" />
    </EditForm>
</div>

@code{
    protected MsgModel _InputMsgModel { get; set; } = new MsgModel();
    protected List<MsgModel> MsgList { get; set; } = new List<MsgModel>();
     protected void SubmitText()
    {
        SendMsg _SendMsg = new SendMsg();
        _SendMsg.MsgText = _InputMsgModel.MsgText;
        MsgList.Add(_SendMsg);
    }
}

现在问题是:在for块中,我应该将_MsgModel传送到组件。同时,我还不知道该如何转让。

您能帮我吗?谢谢。

我正在使用Blazor服务器端创建聊天室。由于接收消息和发送消息的样式不同,我使用Microsoft.AspNetCore.Components创建了一个名为MsgModel的模型。 ...

asp.net-core .net-core blazor
1个回答
0
投票

最后,我找到了解决这个问题的奇怪而愚蠢的方法。

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