I have a view
inside Admin area
and I want to populate it with the following codes but when I run the program my view
shows nothing except empty controls. It returns string
without any problems but have trouble with viewmodels
My viewmodel
is as follow:
public class AddQuestionsViewModel
{
[Required]
[MinLength(20)]
public string Question { get; set; }
[Required]
[MinLength(50)]
public string Answer { get; set; }
public string Category { get; set; }
public int CategoryId { get; set; }
public SelectList Categories { get; set; }
public DateTime DateTime { get; set; }
public bool IsDisplayed { get; set; }
public int UserId { get; set; }
}
My view
:
@model MosahebeClient.Areas.Admin.Models.AddQuestionsViewModel
@{
ViewData["Title"] = "Create";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
<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="Question" class="control-label"></label>
<input asp-for="Question" class="form-control" />
<span asp-validation-for="Question" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Answer" class="control-label"></label>
<input asp-for="Answer" class="form-control" />
<span asp-validation-for="Answer" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Category" class="control-label"></label>
<input asp-for="Category" class="form-control" />
<span asp-validation-for="Category" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CategoryId" class="control-label"></label>
<input asp-for="CategoryId" class="form-control" />
<span asp-validation-for="CategoryId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DateTime" class="control-label"></label>
<input asp-for="DateTime" class="form-control" />
<span asp-validation-for="DateTime" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="IsDisplayed" /> @Html.DisplayNameFor(model => model.IsDisplayed)
</label>
</div>
<div class="form-group">
<label asp-for="UserId" class="control-label"></label>
<input asp-for="UserId" class="form-control" />
<span asp-validation-for="UserId" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
My Controller
:
[Area("Admin")]
public ActionResult Create()
{
AddQuestionsViewModel model = new AddQuestionsViewModel();
model.Categories = new SelectList(_categoryRepository.GetAll(), "Id", "Name");
return View(model);
}
Do you want to add a drop-down list to your view? If so, you can add the following code in your view.
//...
<select asp-items="@Model.Categories">
<option>Please select a option</option>
</select>
//...
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.