gpt4 book ai didi

c# - 如何在 ASP.net MVC 5 中使用 @Html.EditorFor 自动填充表单中的字段?

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:54 25 4
gpt4 key购买 nike

我正在使用 visual studio for asp.net mvc 5 自动生成的表单,将信息保存到数据库中。它是使用带有 Entity Framework 的 asp.net 中的标准脚手架等创建 View 。

我喜欢表单的外观,但我需要一个字段 (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>
}

这部分自动生成的 Controller 如下所示:

// 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);
}

如果您不知道我如何自动填充和/或隐藏表单元素创建日期,能否请您指出我可以从哪里学会自己解决这个问题。我认为我在编程方面是合理的,我只是不太了解 html 助手,或者 Controller 中的绑定(bind)函数。

最佳答案

从你的 View 中删除这部分

 <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);
}

关于c# - 如何在 ASP.net MVC 5 中使用 @Html.EditorFor 自动填充表单中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42045143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com