亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在我的 mvc 項目中沒有序列化此屬性?

為什么在我的 mvc 項目中沒有序列化此屬性?

C#
躍然一笑 2022-08-20 16:20:17
最終目標是序列化 my 中的類型以供 my 使用。abstractViewController我的抽象類型有一個屬性,其名稱對應于具體派生類型的名稱;這就是我將如何確定要選擇哪種具體類型。其值是通過反射在抽象類型的構造函數中設置的:enumenum[JsonConverter(typeof(BlockJsonConverter)]public abstract class Block{   [NotMapped, JsonProperty]   public BlockType BlockType {get; set;}   public string Name {get;set:}   public int Height{get;set;}   public int Width {get;set;}   public int Depth {get;set;}   protected Block(){      BlockType = Enum.TryParse(GetType().Name, out BlockType blocktype)             ?? blocktype : BlockType.Unknown   }}public enum BlockType {   Long, Short, Tall, Unknown}public class Long    : Block { /*...*/ }public class Short   : Block { /*...*/ }public class Tall    : Block { /*...*/ }public class Unknown : Block { /*...*/ }該類由實體框架使用,但該屬性未存儲在數據庫中,因此該屬性用該特性標記;但是,由于我希望將屬性從視圖輪跳到控制器,因此我已使用該屬性對其進行了標記。BlockBlockTypeBlockType[NotMapped][JsonProperty]我已經創建了一個來處理從視圖到控制器的反序列化:TestModelBinderpublic class TestModelBinder : DefaultModelBinder{    protected override object CreateModel(ControllerContext controllerContext,        ModelBindingContext bindingContext, Type modelType)    {        return base.CreateModel(controllerContext, bindingContext,            GetModelType(controllerContext, bindingContext, modelType));    }    protected override ICustomTypeDescriptor GetTypeDescriptor(      ControllerContext controllerContext,ModelBindingContext bindingContext)    {        var modelType = GetModelType(controllerContext, bindingContext, bindingContext.ModelType);        return new AssociatedMetadataTypeTypeDescriptionProvider(modelType)                      .GetTypeDescriptor(modelType);    }    private static Type GetModelType(ControllerContext controllerContext, ModelBindingContext bindingContext,        Type modelType)    {        if (modelType.Name == "Block")        {當我碰到上面的斷點時,綁定Context在其 ValueProvider.FormValueProvider 中沒有我的 BlockType 屬性的 BlockType - 但 Name, Height, Width 和 Depth 屬性按預期列出。...幫助程序只是生成通常的標簽,基于類型(枚舉,字符串等)和驗證消息的編輯器。枚舉的編輯器模板如下:BootstrapEditorGroupFor
查看完整描述

1 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

您的選擇元素似乎沒有呈現該元素。如果沒有該特性,數據將無法與模型中的相應屬性匹配。您可以通過在瀏覽器中編輯HTML并向選擇輸入添加屬性來仔細檢查它,然后嘗試發布表單以查看它是否有效。namenamename="BlockType"



查看完整回答
反對 回復 2022-08-20
  • 1 回答
  • 0 關注
  • 77 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號