0

I have three model Classes(code first)

ProductGroup(p)->product(c)(list of produGroups comes in drop down list while creating a new product),

Now my requirement is,

Product(p)->PreferedLocation(c), when I select preferedLocation(Create action) link ,I have to fill the details for the particular product.Means first I need to display single product name in Label like Product:Mango

In controller

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

In 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>

Here I tried Code for idsplaying product name in label but instead of product name I got output as

product

Product Name.

What modifications I have to do.

1 Answer 1

1

Use DisplayFor instead of LabelFor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.