このコードに問題があります。以前に似たようなものを使用したことがあり、機能していますが、このコードにはありません。理由はわかりません。送信を押すたびに、オブジェクト メッセージが空になります。データベースに送信される値は、たとえば Date などのコントローラーに追加した値だけです。ASP.NET についてあまり知りません。 Googleで自分自身を探すのに失敗したり、正しい用語を見つけたりする
モデル
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" />
}