【问题标题】:How fill custom class in View in asp.net core 2.1如何在asp.net core 2.1的视图中填充自定义类
【发布时间】:2019-02-22 20:48:39
【问题描述】:

我有几个模型,他们就像这样合二为一:

    public class AllData 
    {
    public Category Category {get;set;} // just two string properties ID and Name 
    public SubCategory SubCategory {get;set;} // three string properties ID, Name, Parent_ID
    }

我有一个视图,如果我只是像

那样做标准填充
   <input asp-for="SubCategory.Name" class="form-control" />

他们返回 null,为什么会这样,所有这些都是公开的,并且已经设置

所有视图:

@model CategoryBD.Models.AllData

@{
    ViewData["Title"] = "Create";
}

<h2>Create</h2>

<h4>Subcategory</h4>
<hr />
<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="subcategory.Name" class="control-label"></label>
                <input asp-for="subcategory.Name" class="form-control" />
                <span asp-validation-for="subcategory.Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="subcategory.Parent_ID" class="control-label"></label>
                <input asp-for="subcategory.Parent_ID" class="form-control" />
                <span asp-validation-for="subcategory.Parent_ID" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

控制器:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(AllData subcategory)
    {
        if (ModelState.IsValid)
        {
            _context.Add(subcategory);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Create));
        }
        return View(subcategory);
    }

在控制器中 subcategory.subcategory 始终为 null,即使只创建变量,如:

public SubCategory SubCategory = new SubCategory();

【问题讨论】:

  • 您是否在视图中正确定义了@model?
  • 显示你的html视图代码
  • 使用@Model.SubCategory.Name
  • 它们在哪里返回 null ?分享相关代码和您的预期行为。

标签: c# asp.net razor asp.net-core-mvc asp.net-core-2.1


【解决方案1】:

问题在于模型绑定的一致性。 从视图传递的是一个包含两个 string type 字段的对象,并且您的操作中的参数包含两个 object type 字段。

尝试进行以下代码更改

在视图中

<form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="@Model.SubCategory.Name" class="control-label"></label>
            <input asp-for="@Model.SubCategory.Name" class="form-control" />
            <span asp-validation-for="@Model.SubCategory.Name" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="@Model.SubCategory.Parent_ID" class="control-label"></label>
            <input asp-for="@Model.SubCategory.Parent_ID" class="form-control" />
            <span asp-validation-for="@Model.SubCategory.Parent_ID" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
</form>

控制器:

public async Task<IActionResult> Create(SubCategory subcategory)
    {
    }

子类截图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多