2014-08-28 23 views
0

創建模型的參考,當我面對時,試圖建立我的模型限制的一個參考的一個問題:問題,而無需使用Html.EditorFor()asp.net mvc的

public int RestrictionID 
    public string portefeuille 
    public int AssetID 
    public int SegmentID 
    public int SubAssetID 
    public int Min 
    public int Max 
    public string logic_op 
    public virtual Asset Asset 
    public virtual Segment Segment 
    public virtual SubAsset SubAsset 

也就是說,而不是使用在@ Html.EditorFor創建視圖中的正常模板我正在使用一個下拉列表與後面的腳本來填充下拉取決於先前選擇的項目,很好,但是當提交什麼都沒有發生沒有錯誤沒有重定向,當然,參考不會添加到數據庫中。 這裏是我創建視圖:

<div class="editor-label"> 
     @Html.LabelFor(model => model.portefeuille) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.portefeuille) 
     @Html.ValidationMessageFor(model => model.portefeuille) 
    </div> 

    @Html.DropDownList("Asset", ViewBag.AssetID as SelectList, "Select a Asset Class", new { id="Asset" })<br /> 
      <select id="Segment" name="segment"></select><br /> //Here I am not using Html.EditorFor as EF does 
      <select id="subAsset" name="SubAsset"></select><br /> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Min) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Min) 
     @Html.ValidationMessageFor(model => model.Min) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Max) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Max) 
     @Html.ValidationMessageFor(model => model.Max) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.logic_op) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.logic_op) 
     @Html.ValidationMessageFor(model => model.logic_op) 
    </div> 

和我RestrictionController:(我省略無用部分)

public ActionResult Create() 
    { 
     ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name"); 

     return View(); 
    } 

    // 
    // POST: /Restriction/Create 

    [HttpPost] 
    public ActionResult Create(Restriction restriction) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Restrictions.Add(restriction); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name", restriction.AssetID); 

     return View(restriction); 
    } 

誰能幫助找到問題出在哪裏?

謝謝你的幫助!

+0

沒有人可以幫我嗎?謝謝 – intern 2014-08-28 14:35:48

回答

0

在模型中的屬性的名稱應該是一樣的,以將值綁定在html元素的名稱之後,該帖子back.That是爲什麼下面

<select id="Segment" name="segment"></select><br /> //Here I am not using Html.EditorFor as EF does 
      <select id="subAsset" name="SubAsset"></select><br /> 

代碼應該被替換。

<select id="Segment" name="SegmentID"></select><br /> //Here I am not using Html.EditorFor as EF does 
      <select id="subAsset" name="SubAssetID"></select><br /> 

總是看看任何技術生成的HTML。這將幫助您瞭解事情的工作方式。