【发布时间】: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