我正在使用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.razor
和SendMsg.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的模型。 ...
最后,我找到了解决这个问题的奇怪而愚蠢的方法。