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" />
}
Messageworks 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 objectMessage msg.