0

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

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

现在我的要求是,

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

在控制器中

    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>

在这里,我尝试了在标签中显示产品名称的代码,但我得到的输出不是产品名称

产品

产品名称。

我必须做哪些修改。

4

1 回答 1

1

Use DisplayFor instead of LabelFor.

于 2012-05-19T12:58:48.517 回答