如何将数据绑定到asp.net mvc3中的标签

时间:2012-05-19 12:52:50

标签: asp.net-mvc

我有三个模型类(代码优先)

ProductGroup(p) - >产品(c)(创建新产品时,产品组列表出现在下拉列表中),

现在我的要求是,

产品(P) - > PreferedLocation(c)中, 当我选择preferedLocation(创建动作)链接时,我必须填写特定产品的详细信息。首先我需要在标签中显示单个产品名称,如产品:芒果

在控制器中

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

在视图中:

    <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 个答案:

答案 0 :(得分:1)

使用DisplayFor代替LabelFor。