Reputation: 1449
In the Create view, admins can create a new dish and choose its type from a drop-down list (DishTypes come from database). The problem is I don't know how to design DishesViewModel
, DishesController
and dropdownlist in Create
view.
Here is Dishes model
public class Dishes
{
[Key]
[Required]
public int DishID { get; set; }
[Required]
public string Dishname { get; set; }
public int DishTypeID { get; set; }
[ForeignKey("DishTypeID")]
public virtual DishTypes DishTypes { get; set; }
}
Here is DishTypes model
public class DishTypes
{
[Required]
public int DishTypeID { get; set; }
[Required]
public string DishTypeName { get; set; }
}
This is my currently Dishes view model (I use Automapoper to map them to domain models)
public class DishesVM
{
[Required]
public string DishName{ get; set; }
[Required]
public int DishTypeID{ get; set; }
[Required]
public string DishTypeName { get; set; }
}
This is DishesController
public class DishesController: Controller
{
[HttpGet]
public async Task<IActionResult> Create()
{
// it should pass a list of dishTypes to my view so when admins create a dish they can choose dishtype from a dropdown list
//I down't want to use ViewBag or ViewData
return View()
}
}
Here is Create view
@model DataLayers.Models.ViewModels.DishesVM
<form asp-controller="Dishes" asp-action="Create">
<label class="label" asp-for="Dishname"></label>
<input class="input" type="text" asp-for="Dishname">
//a drop down list, which enables admins choose dishtypes, is needed here
</form
And finally this is DishRepository
public class DishRepository
{
public async Task<IEnumerable<Dishes>> GetAllDishesAsync()
{
return await _RepositoryContext.Set<Dishes>().ToListAsync();
}
}
Feel free to change everything as u wish.
Upvotes: 0
Views: 483
Reputation: 30056
Try steps below:
Change View Model
public class DishesVM
{
[Required]
public string DishName { get; set; }
[Required]
public int DishTypeID { get; set; }
public List<DishTypes> DishTypes { get; set; }
}
View
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="DishName" class="control-label"></label>
<input asp-for="DishName" class="form-control" />
<span asp-validation-for="DishName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DishTypeID" class="control-label"></label>
<select asp-for="DishTypeID"
class="dropdown"
asp-items="@(new SelectList(Model.DishTypes, "DishTypeID" ,"DishTypeName"))"></select>
<span asp-validation-for="DishTypeID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Controller
[HttpGet]
public async Task<IActionResult> Create()
{
var types = await _context.DishTypes.ToListAsync();
var vm = new DishesVM {
DishTypes = types
};
return View(vm);
}
[HttpPost]
public async Task<IActionResult> Create(DishesVM vm)
{
return Ok(vm);
}
Upvotes: 1