I have following class
Basic Class
public class Basic
{
public int ID { get; set; }
public string NAME { get; set; }
}
I have following method to fill the Value to above class
Get Authers Id and Name Method
public IEnumerable<Basic> GetAuthersIdName()
{
.....
}
So in Web API layer Controller Class
I'm getting above details like below
// GET: api/Authers/all
[System.Web.Http.Route("api/Authers/all")]
public IEnumerable<Basic> GetAuthersIdName()
{
return db.GetAuthersIdName();
}
Then I can have following URL to fetch above details
http://localhost:13793/api/Authers/all
So In my MVC Layer , model folder I created a class to handle above details like below
public class LibraryClient
{
private string AUTHER_URL = "http://localhost:13793/api/Authers";
//DropDown
public IEnumerable<Basic> GetAuthersIdName()
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(AUTHER_URL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("Authers/all").Result;
if (response.IsSuccessStatusCode)
return response.Content.ReadAsAsync<IEnumerable<Basic>>().Result;
return null;
}
catch
{
return null;
}
}
}
Then I create Controller class to populate above details on front end like below
Controller Class
public class BooksController : Controller
{
// GET: Books/Create
public ActionResult Create()
{
LibraryClient lc = new LibraryClient();
ViewBag.listAuthers = lc.GetAuthersIdName();
return View("Create");
}
// POST: Books/Create
[HttpPost]
public ActionResult Create(Book book)
{
LibraryClient lc = new LibraryClient();
lc.CreateBook(book);
return RedirectToAction("BookswithAuthers", "BookWithAuther");
}
}
View File
<div class="form-group">
@Html.LabelFor(model => model.Auther_Id, "Auther_Id", htmlAttributes: new { @class = "." })
<div class="col-md-10">
@Html.DropDownList("NAME", (IEnumerable<SelectListItem>)ViewBag.listAuthers, "---Select---");
@Html.ValidationMessageFor(model => model.Auther_Id, "", new { @class = "." })
</div>
</div>
But since I'm using IEnumerable<Basic>
to populate in business layer I cannot use that type in frontend.
I saw many answers do this task using jQuery and Web API , without that path how can I populate my drop down with @Html.DropDownList()