mvc5 html.editorfor,如何在ASP.net MVC 5中使用@ Html.EditorFor自动填充表单中的字段?(How to auto fill fields in a form...

博主在尝试修改ASP.NET MVC5中由Visual Studio自动生成的创建视图,希望日期创建字段能自动填充(最好是隐藏填充)。他们遇到了困难,不理解自动生成的代码,特别是关于Html辅助方法和控制器中的Bind属性。他们正在寻找如何实现这个功能的指导,或者学习理解相关技术的资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm using an autogenerated form from visual studio for asp.net mvc 5 that saves information to a database. It's the create view with the standard scaffolding etc from asp.net with entity framework.

I like the way the form looks, but I need one field (datecreated) to at least be auto filled, (but preferably autofilled and hidden). The problem is I don't understand the autogenerated code at all and my efforts to look it up have not been successful. Nor have my efforts to understand it. I'm still a beginner with html helpers, which I think these are.

Here is the form element I am working with. The part in the middle is the part I need to change to autofill (the date created field), I think the relevant part is changing the EditorFor. but I don't know:

@using (Html.BeginForm())

{

@Html.AntiForgeryToken()

New Patient:

@Html.ValidationSummary(true, "", new { @class = "text-danger" })

... //other form items removed for simplicity

@Html.LabelFor(model => model.DateCreated,"Date Created", htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.DateCreated, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.DateCreated, "", new { @class = "text-danger" })

... //more items left out for simplicity

}

And the auto generated controller for this part looks like this:

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

}

If you dont know how I can autofill and or hide the form element datecreated, could you please point me to where I might learn to figure this out myself. I think I am reasonable at programming, I just don't understand html helpers well, or the bind function in the controller.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值