简体   繁体   中英

how to use a dropdown list in the mvc that is using the data from another table?

<li> @Html.LabelFor(m => m.EmployeeCode)
    @Html.TextBoxFor(m => m.EmployeeCode)
    @Html.ValidationMessageFor(model => model.EmployeeCode)

</li>
<li>
    @Html.LabelFor(m => m.WorkPhone)
    @Html.TextBoxFor(m => m.WorkPhone, new { @placeholder = "WorkPhone" })
    @Html.ValidationMessageFor(model => model.WorkPhone)
</li>
<li>
    @Html.LabelFor(m   => m.BillingRate)
    @Html.TextBoxFor(m => m.BillingRate)
    @Html.ValidationMessageFor(model => model.BillingRate)
</li>
<li>`enter code here`
    @Html.LabelFor(m => m.FunctionId)
    @Html.EditorFor(m => m.FunctionId)
    @Html.ValidationMessageFor(model => model.FunctionId)
</li>
//Here I need the function Id to return values from another model and it sould be as a dropdown !!

You have to pass the other table to the view somehow. To be more precise in terms of programming we're not talking about tables, but about objects.

The 'model' you are using in your code is essentially an object, not a table.

There are basically 2 ways you can pass a different object to your view.

First:

In your controller's action add line:

ViewBag.AnotherObject = YourObject;

than in the view you can access it like that:

@ViewBag.AnotherObject

Second:

Instead of calling

return View(yourModel);

You can call:

return View(new SomeViewModel{
  Model1 = yourModel,
  Model2 = theOtherObject,
})

SomeViewModel is a class you have to create yourself.

Than in your view you can access it like that: @Model.Model1.EmployeeCode @Model.Model2.WhateverYouHaveHere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM