我正在尝试将来自网址的ID存储在textboxfor()
中,但我没有获得结果。当我在文本框中输入“id”时,我获得了正确的结果。而是在textboxfor(model=> model.IDCantiere,new{@value="id"})
中。它没有运行;请帮助我,我附上了代码片段:
Controller.cs
// GET: CantiereOres/Create
public ActionResult Create(int id)
{
ViewBag.id = id;
//ViewBag.IdCantiere = new SelectList(db.Cantieres, "IdCantiere", "IdCantiere");
ViewBag.IdPersona = new SelectList(db.CantierePersones, "IdPersona", "Persona");
return View();
}
// POST: CantiereOres/Create
// Per proteggere da attacchi di overposting, abilitare le proprietà a cui eseguire il binding.
// Per ulteriori dettagli, vedere https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IdOre,IdCantiere,IdPersona,Ore,datetime")] CantiereOre cantiereOre)
{
if (ModelState.IsValid)
{
db.CantiereOres.Add(cantiereOre);
db.SaveChanges();
return RedirectToAction("details", "Cantieres", new { id = cantiereOre.IdCantiere });
}
ViewBag.IdCantiere = new SelectList(db.Cantieres, "IdCantiere", "IdCantiere", cantiereOre.IdCantiere);
ViewBag.IdPersona = new SelectList(db.CantierePersones, "IdPersona", "Persona", cantiereOre.IdPersona);
return View(cantiereOre);
}
View.cshtml:
@model archeagroup.Models.CantiereOre
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.TextBox("id")
<div class="form-horizontal">
<h4>CantiereOre</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.IdCantiere, "IdCantiere", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model=>model.IdCantiere, new {@value ="id", @class = "form-control" } )
@*@Html.EditorFor(model => model.IdCantiere, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.IdCantiere, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IdPersona, "IdPersona", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("IdPersona", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.IdPersona, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Ore, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Ore, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Ore, "", 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", "Details", "Cantieres")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:1)
如果你想让2441在这个文本框中吗?
@Html.TextBoxFor(model=>model.IdCantiere, new {@value ="id", @class = "form-control" } )
然后在你的控制器中,而不是这个:
ViewBag.id = id;
这样做:
Model.IdCantiere = id;
将文本框更改为:
@Html.TextBoxFor(model=>model.IdCantiere, new { @class = "form-control" })
将(在您的屏幕截图中)2441放在您指定的文本框中。
编辑:所以你需要从你的控制器返回一个archeagroup.Models.CantiereOre的实例创建方法:
public ActionResult Create(int id)
{
var model = new acheagroup.Models.CantiereOre();
model.IdCantiere = id;
model... //other assignments - use this instead of ViewBag
return View(model);
}
有了这个,上面的代码将起作用,因为它将从模型中提供。