簡體   English   中英

我的視圖模型沒有填充管理區域內的視圖

[英]My viewmodel doesn't populate the view inside Admin Area

我在管理area內有一個view ,我想用以下代碼填充它,但是當我運行程序時,我的view除了空控件外什么都不顯示。 它返回string沒有任何問題,但在viewmodels方面有問題

我的viewmodel模型如下:

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; }
}

我的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>

我的Controller

[Area("Admin")] 
public ActionResult Create() 
{
    AddQuestionsViewModel model = new AddQuestionsViewModel();
    model.Categories = new SelectList(_categoryRepository.GetAll(), "Id", "Name");
    return View(model); 
}

您想在視圖中添加下拉列表嗎? 如果是這樣,您可以在視圖中添加以下代碼。

 //...
 <select asp-items="@Model.Categories">
    <option>Please select a option</option>
 </select>
 //...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM