我使用visual studio for asp.net mvc 5的自动生成表单将信息保存到数据库中。它是带有标准脚手架等的创建视图,来自带有实体框架的asp.net。
我喜欢表单的外观,但我需要一个字段(datecreated)至少自动填充(但最好是自动填充和隐藏)。问题是我根本不了解自动生成的代码,我查找的努力并没有成功。我也没有努力去理解它。我仍然是一个有html助手的初学者,我认为这些是。
这是我正在使用的表单元素。中间部分是我需要更改为自动填充的部分(创建日期字段),我认为相关部分正在改变EditorFor。但我不知道:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>New Patient:</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
... //为简单起见删除了其他表单项
<div class="form-group">
@Html.LabelFor(model => model.DateCreated,"Date Created", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateCreated, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateCreated, "", new { @class = "text-danger" })
</div>
</div>
... //为简单起见省略了更多项目
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
此部分的自动生成控制器如下所示:
// GET: Subjects/Create
public ActionResult Create()
{
return View();
}
// POST: Subjects/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,DOB,Male,Female,Address,City,ZIP,PhoneHome,PhoneCell,Email,EmergencyContact,EmergencyContactPhone,EmergencyContactRelationship,ReferredBy,DateCreated,Allergy,AllergyDescription,HighBloodPressure,LowBloodPressure,HeartCondition,Diabetes,Anemia,HighCholesterol,Pacemaker,Epilepsy,Pregnant,Cancer,STD,Pain,PainDescription,Headache,HeadacheDescription,CommonCold,HighBloodPressureConcern,Stress,Depression,Sleep,Menstruation,Fertility,WeightControl,Other")] Subject subject)
{
if (ModelState.IsValid)
{
db.SubjectDatabase.Add(subject);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(subject);
}
如果您不知道我如何自动填充和隐藏表单元素datecreated,请您指点我自己可以学习如何解决这个问题。我认为我在编程方面很合理,我只是不太了解html帮助器,或者控制器中的绑定功能。
答案 0 :(得分:2)
从您的视图中删除此部分
<div class="form-group">
@Html.LabelFor(model => model.DateCreated,"Date Created", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateCreated, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateCreated, "", new { @class = "text-danger" })
</div>
</div>
然后,在Controller内部从Bind属性中删除DateCreated并指定DateCreated属性:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,DOB,Male,Female,Address,City,ZIP,PhoneHome,PhoneCell,Email,EmergencyContact,EmergencyContactPhone,EmergencyContactRelationship,ReferredBy,Allergy,AllergyDescription,HighBloodPressure,LowBloodPressure,HeartCondition,Diabetes,Anemia,HighCholesterol,Pacemaker,Epilepsy,Pregnant,Cancer,STD,Pain,PainDescription,Headache,HeadacheDescription,CommonCold,HighBloodPressureConcern,Stress,Depression,Sleep,Menstruation,Fertility,WeightControl,Other")] Subject subject)
{
if (ModelState.IsValid)
{
subject.DateCreated = DateTime.Now; //if you want UTC time, use DateTime.UtcNow
db.SubjectDatabase.Add(subject);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(subject);
}
答案 1 :(得分:0)
@Value也可用于使用@ Html.EditorFor预填充斑点:
@ Html.EditorFor(c =&gt; c.Propertyname,new {@Value =&#34; 5&#34;})
在此stackoverflow问题中可以找到更多信息: Html.EditorFor Set Default Value