public class AssetViewModel { public int ID { get; set; } public string AssetName { get; set; } public IEnumerable<Department> Departments = Enum.GetValues(typeof(Department)).OfType<Department>(); public IList<Country> Countries { get; set; } public string CountryName { get; set; } public string EMailAdress { get; set; } public DateTime PurchaseDate { get; set; } public bool broken = false; }
public class AssetController : Controller { public ActionResult Create() { var task = countryRepository.GetAllCountries(new Uri(countriesApiUrl + "regionalbloc/EU")); var model = new AssetViewModel { Countries = task.Result }; return View(model); } // POST: AssetController/Create [HttpPost] public ActionResult Create(AssetViewModel assetViewModel) { //all assetViewModel attributes are null } }
@model Domain.Models.ViewModels.AssetViewModel @{ ViewData["Title"] = "Create"; } <h1>Create</h1> <h4>AssetViewModel</h4> <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="Create" method="post" > <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="AssetName" class="control-label"></label> <input asp-for="AssetName" class="form-control" /> <span asp-validation-for="AssetName" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Department" class="control-label"></label> <select asp-for="Department"> @foreach (var item in Model.Departments) { <option value="@item">@item</option> } </select> <span asp-validation-for="Department" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="CountryName" class="control-label"></label> <select asp-for="CountryName" class="form-control"> @foreach (var item in Model.Countries) { <option value="@item.Name">@item.Name</option> } </select> <span asp-validation-for="CountryName" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="EMailAdress" class="control-label"></label> <input asp-for="EMailAdress" class="form-control" /> <span asp-validation-for="EMailAdress" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="PurchaseDate" class="control-label"></label> <input asp-for="PurchaseDate" class="form-control" /> <span asp-validation-for="PurchaseDate" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> </form> </div> </div> <div> <a asp-action="Index">Back to List</a> </div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
Countries = task.Result
async
await
public async Task<ActionResult> Create() { var countries = await countryRepository.GetAllCountries(new Uri(countriesApiUrl + "regionalbloc/EU")); var model = new AssetViewModel { Countries = countries }; return View(model); }
[HttpPost]
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)