我想问一下,有没有办法在__repr__
HttpPost方法中为属性DateTime.Now
和ENTRY_DATE
设置自动AUDIT_TIME
值?表单已创建,工作正常。如果手动插入DateTime。但是,如果我设置一个自动值并且会遇到
“一个或多个验证错误”..
这是我的模型(我不明白如何制作视图模型):
Create()
这是我的观点
public partial class TRRESPONDENT
{
public TRRESPONDENT()
{
this.TRFOLLOWUPRESPONDENTs = new HashSet<TRFOLLOWUPRESPONDENT>();
}
[Required(ErrorMessage = "Respondent ID is required!")]
[Display(Name = "Respondent ID")]
public string RESPONDENT_ID { get; set; }
[Required(ErrorMessage = "Please select a BINUS Center!")]
[Display(Name = "Binus Center")]
public string BC_ID { get; set; }
[Required(ErrorMessage = "Name cannot be empty!")]
[Display(Name = "Name")]
[StringLength(100,ErrorMessage = "Name length cannot be more than 100 characters!")]
public string FULL_NAME { get; set; }
.... // more properties
[Required(ErrorMessage = "Please pick a City Location!")]
[Display(Name = "City")]
public int CITY_ID { get; set; }
// The following 2 properties need to be set
[Display(Name = "Entry Date")]
public DateTime ENTRY_DATE { get; set; }
public DateTime AUDIT_TIME { get; set; }
....
public virtual LTCITY LTCITY { get; set; }
public virtual LTSOURCERESPONDENT LTSOURCERESPONDENT { get; set; }
public virtual MSBINUSCENTER MSBINUSCENTER { get; set; }
public virtual ICollection<TRFOLLOWUPRESPONDENT> TRFOLLOWUPRESPONDENTs { get; set; }
}
这是我的控制者:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.LabelFor(model => model.RESPONDENT_ID)
@Html.EditorFor(model => model.RESPONDENT_ID)
@Html.ValidationMessageFor(model => model.RESPONDENT_ID)
@Html.LabelFor(model => model.BC_ID, "Binus Center")
@Html.DropDownList("BC_ID", null)
@Html.ValidationMessageFor(model => model.BC_ID)
@Html.LabelFor(model => model.FULL_NAME)
@Html.EditorFor(model => model.FULL_NAME)
@Html.ValidationMessageFor(model => model.FULL_NAME)
.... // more form controls
@Html.LabelFor(model => model.CITY_ID, "City")
@Html.DropDownList("CITY_ID", null)
@Html.ValidationMessageFor(model => model.CITY_ID)
@Html.HiddenFor(model => model.ENTRY_DATE)
@Html.HiddenFor(model => model.AUDIT_TIME)
<input type="submit" value="Create" class="btn btn-default" />
}
答案 0 :(得分:1)
您尚未说明错误消息的详细信息,但毫无疑问,这是因为您将01/01/0001
的值保存到DATETIME
的字段(仅接受{{1}之间的日期} 01/01/1753
)而非12/31/9999
。
您的日期值为DATETIME2
(01/01/0001
的默认值)的原因是您没有将模型传递给视图,因此使用默认值。您的GET中的代码必须是
DateTime
但是,您不应在视图中使用数据模型,而是创建仅包含所需属性的视图模型。只应在将数据模型保存到数据库之前立即设置public ActionResult Create()
{
ViewBag.CITY_ID = new SelectList(db.LTCITies, "CITY_ID", "CITY_NAME", 1);
var entry = new Models.TRRESPONDENT
{
ENTRY_DATE = DateTime.Now,
AUDIT_TIME = DateTime.Now,
};
return View(entry); // return your model
}
等值。有关创建视图模型的信息,请参阅What is ViewModel in MVC?。
创建视图模型的基本步骤是
ENTRY_DATE
并将数据模型复制到和
并重命名(例如)ViewModels
RespondentVM
属性(它们是
查看特定属性)[Display]
和ENTRY_DATE
)除了属性
是应该重命名为AUDIT_TIME
的对象ID
假设你使用默认路线自动绑定(注意它
不清楚你是否有ID属性 - 我认为它
ID
,但应该是自动递增的RESPONDENT_ID
数据库-i.e. int
)。我也
建议您重命名所有属性以遵循命名
约定 - [Key]public int RespondentId { get; set; }
,而不是EntryDate
。ENTRY_DATE
并添加nullable
用于防止欠发布攻击的属性(例如[Required]
变为public int
CITY_ID { get; set; }
public int? CityID { get; set; }
等添加其他属性
目前正在分配给SelectList
个属性,例如ViewBag
您的控制器方法将是
public
IEnumerable<SelectListItem> CityList { get; set; }
还有一些关于观看代码的额外说明。
public ActionResult Create()
{
RespondentVM model = new RespondentVM();
ConfigureViewModel(model);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(RespondentVM model) // note a [Bind]` attribute is not required
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
// Initialize an instance of your data model and set its properties based on the view model
TRRESPONDENT respondent = new TRRESPONDENT()
{
FULL_NAME = model.FullName,
CITY_ID = model.CityID,
....
// Set default values
ENTRY_DATE = DateTime.Now,
....
}
db.TRRESPONDENTs.Add(respondent);
db.SaveChanges();
return RedirectToAction("Index");
}
// Common code for populating SelectLists etc - DRY!
private void ConfigureviewModel(RespondentVM model)
{
// Note - delete the 4th parameter of the SelectList constructor
model.CityID = new SelectList(db.LTCITies, "CITY_ID", "CITY_NAME");
}
,则不需要ID属性的隐藏输入
和你使用默认路由ID
个属性,因此在视图中
它只是[Display(Name = "..")]
,而不是
@Html.LabelFor(m => m.PropertyName)
@Html.LabelFor(m => m.PropertyName, "some text")
- 您
当前实现不会给出正确的双向模型绑定或
客户端验证。