【问题标题】:ASP.NET Core MVC empty object from response来自响应的 ASP.NET Core MVC 空对象
【发布时间】:2021-01-16 00:21:27
【问题描述】:

我在使用这段代码时遇到了一些问题,我以前使用过类似的东西并且正在工作,但它不在这个我不知道为什么。每次我点击提交时,对象消息都是空的,提交到数据库中的唯一值是我在控制器中添加的值,例如 Date,我对 ASP.NET 了解不多,不知道为什么在谷歌上寻找自己的失败也没有正确的条款

型号

public class Message {
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }
        [BsonElement("date")]
        public string Date;
        [BsonElement("sender")]
        public string Sender;
        [BsonElement("body")]
        public string Body;
        [BsonElement("type")]
        public string Type;
        [BsonElement("subject")]
        public string Subject;
    }

控制器


[HttpPost]
public IActionResult Write(Message msg) {
    Mongo.Instance.InsertMessage(new Message() {
        Subject = msg.Subject,
        Body = msg.Body,
        Date = DateTime.Now.ToString("dd/MM/yyyy"),
        Type = msg.Type,
        Sender = "None"
    });
    return RedirectToAction("Index", "Home");
}

表格

 @using (Html.BeginForm("Write", "CCG", FormMethod.Post)) {


            <div class="form-group">
                <p>Subject</p>
                @Html.TextBoxFor(model => model.Subject)
            </div>

            <div class="form-group">
                @{
                    List<SelectListItem> items = new List<SelectListItem>();
                    items.Add(new SelectListItem() {
                        Text = "Petition",
                        Value = "petition"
                    });
                    items.Add(new SelectListItem() {
                        Text = "Congratulate",
                        Value = "congratulate"
                    });
                    items.Add(new SelectListItem() {
                        Text = "Offer",
                        Value = "offer"
                    });
                }
                @Html.DropDownListFor(model => model.Type, items)
            </div>

            <div class="form-group">
                <p>Body</p>
                @Html.TextAreaFor(model => model.Body)
            </div>

            <input type="submit" value="Send" />
        }

【问题讨论】:

  • 我看不到@model 在您的视图中的位置,请显示您的创建表单操作。
  • 如果您指定 Message 的哪些属性有效以及预期有效的属性会有所帮助,屏幕截图会有所帮助。提示:你应该在你的控制器上设置一个断点并读取对象Message msg真正提交的属性。
  • @model NuCloudWeb.Models.Message @{ ViewData["Title"] = "Message"; } 我在顶部有那个

标签: c# asp.net-mvc razor


【解决方案1】:

我很确定你需要通过添加 getset 方法将这些字段变成属性。

例如

    public class Message {
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }
        [BsonElement("date")]
        public string Date { get; set; }
        [BsonElement("sender")]
        public string Sender { get; set; }
        [BsonElement("body")]
        public string Body { get; set; }
        [BsonElement("type")]
        public string Type { get; set; }
        [BsonElement("subject")]
        public string Subject { get; set; }
    }

【讨论】:

    【解决方案2】:

    为 API 对象创建模型类时,请确保始终拥有 getter 和 setter。 :) 快乐编码

    public class Activity
    {
        public Guid Id { get; set; }
        public string ActivityName { get; set; }
        public string ActivityDescription { get; set; }
        public string ActivityLink { get; set; }
        public string ActivityVenue { get; set; }
        public bool IsActive { get; set; }
        public DateTime DateCreated { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多