【问题标题】:Setting properties using asp-for in MVC Core App with EF Core?在带有 EF Core 的 MVC Core App 中使用 asp-for 设置属性?
【发布时间】:2020-07-29 07:37:50
【问题描述】:

我从这些文档中了解到:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-3.1,asp-for 用于将值从输入元素传输到后端 C# 类属性,例如:

<input type="text" id="wsite" name="wsite" maxlength="11" asp-for="WebsiteName">

连同'@folderName ClassName;'在顶部,让您转移到此示例属性:

public string WebsiteName { get; set; }

但是,使用 console.WriteLine 对此进行测试表明,在提交包含输入的表单后,该属性仍然为空。知道我错过了什么吗?

编辑:更新以显示我的属性名称和 asp-for 值匹配,并添加我的控制器:

    [HttpPost]
    public IActionResult Post()
    {
        DBCRUD.Initialize(_context);
        return NoContent();
    }

【问题讨论】:

  • 您能否向我们展示将接收提交的数据的控制器操作?
  • 更新了我原来的帖子以显示控制器。

标签: asp.net-core asp.net-core-mvc ef-core-3.1


【解决方案1】:

asp-for 标记应与变量名匹配。

尝试定义您的 html 表单,例如:

@model Classname
<form asp-action="ActionName" asp-controller="ControllerName" ...>

<input type="text" asp-for="VarName">

和你的控制器:

public MyReturnVariable ActionName(ClassName class) {
    Console.WriteLine(class.VarName);
}

【讨论】:

  • 更新了我的原始帖子以显示控制器。在控制器中使用 Console.WriteLine 也会出现“无法访问的代码”错误。
【解决方案2】:

标签助手用于模型绑定以及在网页中创建和呈现 HTML 元素(显示模型属性)。

所以,在网页(或视图页面)中,在页眉的顶部,我们应该添加以下代码来分配模型。

@model MVCSample.Models.BookModel

然后,使用以下代码显示属性:

<div class="row">
    <div class="col-md-4">
        <form asp-action="AddBook">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="ID" class="control-label"></label>
                <input asp-for="ID" class="form-control" />
                <span asp-validation-for="ID" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="bookName" class="control-label"></label>
                <input asp-for="bookName" class="form-control" />
                <span asp-validation-for="bookName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Title" class="control-label"></label>
                <input asp-for="Title" class="form-control" />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

控制器中的代码:

    [HttpGet] 
    public IActionResult AddBook()
    {
        BookModel book = new BookModel()
        {
            ID = 1001,
            bookName = "War and Peace",
            Title = "War and Peace"
        };
        return View(book);
    }

模型中的代码:

public class BookModel
{
    public int ID { get; set; }
    public string bookName { get; set; } 
    public string Title { get; set; }
}

更多详细信息,您可以查看Model Binding

【讨论】:

  • 您添加的链接似乎特定于 Razor Pages,我没有使用它。您的答案中还有很多多余的代码。不过,感谢您的回复。
  • 编辑:我现在明白模型绑定可以在 Razor 页面之外使用,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多