简体   繁体   中英

How to create HtmlDropdownListfor in asp.net mvc 5 without use of modelviews in one to many relationship

I am a beginner and learning ASP.Net MVC 5 and there are many answers in stack overflow related to my question but I could not find what I was looking for.

So, I have two model. Car and Company . one car can belong to one company and one company can have many cars . Now I have form named " Car Entry Form " where user will see a textbox to put car name and a dropdown list to select company name .

Question: I am able to create a view model and display the form with dropdown of company list. But I just want to know suppose if I am not allowed to use viewmodel , in that situation is it possible to show the dropdown list in the Car Entry Form. Is it mandatory/preferrable to use viewmodel , in my above stated scenario. Can I just use car model and somehow incorporate the dropdown list for company? Am I making sense?

Viewmodel solution

Car Model

public class Car
{
    public int Id { get; set; }
    public string CarName { get; set; }
    public int CompanyId { get; set; }
    public virtual Company Company { get; set; }
}

Company Model

public class Company
{
    public int Id { get; set; }
    public string CompanyName { get; set; }
    public ICollection<Website> Car { get; set; }
}

ViewModel

 public class CarViewModel
{
    public int Id { get; set; }
    public IEnumerable<Company> Companies { get; set; }
    public int CompanyId { get; set; }
    public string CarName { get; set; }
}

Controller Action

public ActionResult Create()
{
    var viewModel = new CarViewModel
    {
        Companies = _context.Companies.ToList()
    };
    return View(viewModel);
}

Fluent Api

modelBuilder.Entity<Company>()
                .HasMany<Car>(w => w.Cars)
                .WithRequired(w => w.Company)
                .HasForeignKey(w => w.CompanyId)
                .WillCascadeOnDelete(false);

View File

@model WebApplication3.Models.CarFormViewModel
        <div class="form-group">
    <div class="col-md-10">
    @Html.DropDownListFor(m => m.CompanyId, new SelectList(Model.Companies, "Id", "Name", Model.Companies), new { @class = "form-control"})
    </div>
</div>

Yes it is possible.

I often use approach like this:

Your action should be like this:

public ActionResult Create()
{
    ViewBag.Companies = _context.Companies.ToList();
    return View();
}

And Here is Your View:

@model WebApplication3.Models.Car
    <div class="form-group">
<div class="col-md-10">
@Html.DropDownListFor(m => m.CompanyId, new SelectList(ViewBag.Companies as List<WebApplication3.Models.Company>, "Id", "Name", Model.Companies), new { @class = "form-control"})
</div>

ViewBag is dynamic object and it is used to pass objects form controller to the view.

can find more about ViewBag here .

And I belive when you are creating a view to edit Car Model its ViewModel should be Car and a CompanyList is not a part of a Car it's something that just help user to edit car. you know, you just need a CompanyId for editing or creating a Car so it's better to don't use it as part of ViewModel. But feel free and find your best practices and approaches. there is no rule to how separate your objects that want to pass to view.

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