Ich versuche, eine ID von URL in einer textboxfor()
zu speichern, aber ich erhalte das Ergebnis nicht. Wenn ich "id" in ein Textfeld einfüge, erhalte ich das korrekte Ergebnis. Stattdessen in einem textboxfor(model=> model.IDCantiere,new{@value="id"})
. Es läuft nicht; Bitte helfen Sie mir, ich habe die Code-Fragmente angehängt:Wie könnte ich eine "ID" (urlparameter.optional) durch eine Textbox for()
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")
}
Sorry Brian Ich bin neu in MVC, wenn ich Controller-Code ändern, wie Sie vorschlagen Ich erhalte Fehler CS0103 Name 'Modell' existiert nicht im aktuellen Kontext, wo sind meine Fehler – Alberto
Ich versuche, eine 'öffentliche int IdCantiere { bekommen; einstellen; } 'aber nichts passiert – Alberto
Sie müssen eine Modellklasse aus der Ansicht zurückgeben, wie in meiner Bearbeitung oben. –