2017-04-30 5 views
2

URLからのIDをtextboxfor()に保存しようとしていますが、結果が得られません。 "id"をテキストボックスに入れると、正しい結果が得られます。代わりに、textboxfor(model=> model.IDCantiere,new{@value="id"})にあります。実行されません。テキストボックス()で「id」(urlparameter.optional)を保存する方法

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

答えて

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 ated。

EDIT:これにより

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

それはモデルから供給されますので、上記のコードは動作します:だから、メソッドを作成し、あなたのコントローラからarcheagroup.Models.CantiereOreのインスタンスを返すようにする必要があります。

+0

申し訳ありませんが、あなたが私がエラーを取得するsuggetsとしてコントローラコードを変更する場合、私はMVCの新しいですCS0103名前 'モデル'は現在のコンテキストに存在しません、私のミスはどこですか? – Alberto

+0

私は 'public int IdCantiere {取得する;セット; } 'しかし何も起こらない – Alberto

+0

上記の私の編集のように、ビューからモデルクラスを返す必要があります。 –

関連する問題