gpt4 book ai didi

asp.net-mvc - MVC 中的默认值在 http 发布之前创建 View

转载 作者:行者123 更新时间:2023-12-02 04:39:33 25 4
gpt4 key购买 nike

您好,我正在尝试在创建 View 中使用“DateTime.Now”默认日期。并将“事件”字段设置为“真”

以下代码在 Controller 的以下操作中执行此操作:

// POST: RequestTypes/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 async Task<ActionResult> Create([Bind(Include = "Id,RequestTypeDescription,LastUpdated,Active,Team")] RequestType requestType)
{
if (ModelState.IsValid)
{
db.RequestTypes.Add(requestType);
requestType.LastUpdated = System.DateTime.Now;
requestType.Active = true;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}

ViewBag.Team = new SelectList(db.Teams, "Id", "TeamDescription", requestType.Team);
return View(requestType);
}

这是保存时默认的 httppost 代码。

我想做的是默认这些字段,以便它们在创建 View 首次启动时显示 - 在以下操作中使用以下代码:

// GET: RequestTypes/Create
public ActionResult Create()
{
ViewBag.Team = new SelectList(db.Teams, "Id", "TeamDescription");
ViewBag.LastUpdated = System.DateTime.Now;
return View();
}

我的Create View代码是MVC脚手架创建的标准:

@model ManageHR5.Models.RequestType

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>RequestType</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.RequestTypeDescription, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RequestTypeDescription, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RequestTypeDescription, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.LastUpdated, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastUpdated, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastUpdated, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Active)
@Html.ValidationMessageFor(model => model.Active, "", new { @class = "text-danger" })
</div>
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Team, "Team", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Team", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Team, "", 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>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")}

当我运行应用程序并启动创建页面时,启动创建页面时不会显示默认值。

我不明白什么?

(MVC 的新手 :( )

最佳答案

您在 Get 上的模型中设置它们以用于显示目的

// GET: RequestTypes/Create
public ActionResult Create() {
ViewBag.Team = new SelectList(db.Teams, "Id", "TeamDescription");
var model = new RequestType();
model.LastUpdated = System.DateTime.Now;
model.Active = true;
return View(model);
}

但是就像提供的评论中指出的那样

You set them immediately before you save the object in the POST method

关于asp.net-mvc - MVC 中的默认值在 http 发布之前创建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557305/

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