2012-05-19 53 views
0

我有三個模型類(代碼第一)如何將數據綁定在asp.net MVC3標記

ProductGroup(P) - >(c)產物produGroups的(名單進來下拉列表中,同時創建一個新的產品),

現在,我的要求是,

產品(p) - > preferedLocation(C), 當我選擇preferedLocation(創建行動)的鏈接,我必須填寫的特定product.Means細節首先,我需要在產品標籤中顯示單個產品名稱:芒果

在控制器

public ActionResult PreferedLocation() 
    { 
     ViewBag.ProductID = new SelectList(db.Products,"ProductID","Name"); 
     //ViewBag.CountryID = new SelectList(db.Countries, "CountryID", "Name"); 
     return View(); 

    } 

    // 
    // POST: /PreferedLocation/Create 

    [HttpPost] 
    public ActionResult PreferedLocation(PreferredLocation preferredlocation) 
    { 
     if (ModelState.IsValid) 
     { 
      db.PreferredLocations.Add(preferredlocation); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     // ViewBag.ProductID = new SelectList(db.Products, "ProductID", "Name", preferredlocation.ProductID); 

     return View(preferredlocation); 
    } 

在View:

<div class="editor-field"> 
     <%: Html.EditorFor(model => model.Name) %> 

     <%: Html.ValidationMessageFor(model => model.Name) %> 
    </div> 

    <div class="editor-label"> 
     <%: Html.LabelFor(model => model.StreetAdress) %> 
    </div> 
    <div class="editor-field"> 
     <%: Html.EditorFor(model => model.StreetAdress) %> 
     <%: Html.ValidationMessageFor(model => model.StreetAdress) %> 
    </div> 

    <div class="editor-label"> 
     <%: Html.LabelFor(model => model.Description) %> 
    </div> 
    <div class="editor-field"> 
     <%: Html.EditorFor(model => model.Description) %> 
     <%: Html.ValidationMessageFor(model => model.Description) %> 
    </div> 

    <div class="editor-label"> 
     <%: Html.LabelFor(model => model.Latitude) %> 
    </div> 
    <div class="editor-field"> 
     <%: Html.EditorFor(model => model.Latitude) %> 
     <%: Html.ValidationMessageFor(model => model.Latitude) %> 
    </div> 

      <div class="display-label">product</div> 
<div class="display-field"> 
    <%: Html.LabelFor(model=>Model.Product.Name) %> 
    <%: Html.ValidationMessageFor(model => model.ProductID) %> 
</div> 

在這裏,我試圖代碼在標籤 idsplaying產品的名稱,但不是產品的名字,我得到了輸出

產品

產品名稱。

我必須做些什麼修改。

回答

1

使用DisplayFor替代LabelFor。

相關問題