2014-08-28 21 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。这将帮助您了解事情的工作方式。