简体   繁体   English

ASP.NET 核心 MVC 为空 object 来自响应

[英]ASP.NET Core MVC empty object from response

I'm having some trouble with this code, I have used something similar before and is working but it isn't on this one I don't know why.我在使用这段代码时遇到了一些问题,我以前使用过类似的东西并且正在工作但它不在这个代码上我不知道为什么。 Each time I hit submit the object message comes empty, the only values that are submitted into the database are the ones that I add in the controller like the Date for example, I don't know much about ASP.NET to know why is it failing nor the correct terms to look for myself on google每次我点击提交 object 消息都是空的,提交到数据库中的唯一值是我在 controller 中添加的值,例如日期,我对 ASP.NET 了解不多,不知道为什么它会失败,也不会在谷歌上寻找自己的正确条款

Model Model

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

Controller Controller


[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");
}

Form形式

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

I'm pretty sure uou need to make those fields into properties by adding get and set methods.我很确定你需要通过添加getset方法将这些字段变成属性。

eg例如

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

When creating a model class for API objects make sure always have the getter and setter.在为 API 对象创建 model class 时,确保始终具有 getter 和 setter。 :) Happy Coding :) 快乐编码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM