0

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

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


[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" />
        }
3
  • I don't see where is @model in your view and please show your create form action. Commented Jan 16, 2021 at 1:13
  • It would help if you specified what properties of Message works and what was expected to work, screenshots would help. Tip: you should put a breakpoint on your controller and read the properties of what really got submitted for object Message msg. Commented Jan 16, 2021 at 1:21
  • @model NuCloudWeb.Models.Message @{ ViewData["Title"] = "Message"; } I have that on the top Commented Jan 16, 2021 at 2:25

2 Answers 2

2

I'm pretty sure uou need to make those fields into properties by adding get and set methods.

e.g.

    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; }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

When creating a model class for API objects make sure always have the getter and 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; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.