简体   繁体   English

Tag Helper 表单:asp-for 未按预期工作

[英]Tag Helper forms: asp-for not working as expected

Thanks for the help so far.感谢你目前的帮助。 I've worked to make sure everything else works so I can focus on this problem.我一直在努力确保其他一切正常,这样我就可以专注于这个问题。 I'm still convinced it'll be an easy fix once we've cracked it.我仍然相信,一旦我们破解了它,它就会很容易修复。 I have the following code, sorry I changed it so much, I had to start again after I made a real mess of the last one without taking a backup.我有以下代码,抱歉,我改变了太多,在我把最后一个代码搞得一团糟之后,我不得不重新开始,没有进行备份。

        public IActionResult Index()
    {

        if(IndexModel.GlobalTasks == null)
        {
            IndexModel initModel = new IndexModel();
            initModel.AllTasks = InitList();
            initModel.EmptyTask = new ToDoTask();
            IndexModel.GlobalTasks = initModel.AllTasks;
        }
        IndexModel model = new IndexModel();
        model.AllTasks = IndexModel.GlobalTasks;
        model.EmptyTask = new ToDoTask("");
        return View(model);
    }
    //Create Task
    public IActionResult Create(ToDoTask indexModel)
    {
        IndexModel.GlobalTasks.Add(indexModel);
        return RedirectToAction("Index");
    }

And:和:

@model DE.Models.IndexModel
<h2>To Do List</h2>
<form asp-action="Create">
    <input asp-for="EmptyTask" value="@Model.EmptyTask" />
    <input asp-for="EmptyTask.TaskDetails" placeholder="New Task" />
    <button type="submit">Add Task</button>
</form>

The good news is this creates a new ToDoTask.好消息是这会创建一个新的 ToDoTask。 So the Controller code must be pretty close to spot on.所以控制器代码必须非常接近现场。 The problem is the View is passing null details to the controller, so I'm getting an empty Task, which isn't what I want.问题是视图将空详细信息传递给控制器,所以我得到一个空任务,这不是我想要的。 Any ideas?有任何想法吗?

在此处输入图像描述

Using Tag Helper forms:使用 Tag Helper 表单:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    
<section >
    <input type="text" asp placeholder="Title" asp-for="MyWork.Title"/>
</section >

Your controller action expects a ToDoTask object while your view uses a TaskViewModel object.您的控制器操作需要一个ToDoTask对象,而您的视图使用一个TaskViewModel对象。

Try using the same type in both of them.尝试在两者中使用相同的类型。

In your Create() method you need to instantiate the ToDoTask object, I think.我认为,在您的Create()方法中,您需要实例化ToDoTask对象。 So try this:所以试试这个:

[HttpPost]
public IActionResult Create(ToDoTask newTask)
{
    newTask = new ToDoTask(); 

    return RedirectToAction("Index");
}

You may also need to return the ToDoTask object.您可能还需要返回ToDoTask对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 .NET Core 的 Material Design Lite 中的 Asp-for 标签助手 - Asp-for tag helper in Material Design Lite with .NET Core ASP.NET 核心 - 如何在另一个自定义标签助手中从 asp-for 标签助手获取输入 ID - ASP.NET Core - How to get the input ID from asp-for tag helper in another custom tag helper 为什么 asp-for 输入标签助手总是使用表单数据而不是视图模型? - Why asp-for input tag helper always use form data instead of the viewmodel? 输入的标签助手[asp-for]与字节的数组值不兼容(与@ Html.HiddenFor相反) - tag helper [asp-for] for the input doesn't work with byte's array values (opposite to @Html.HiddenFor) 当模型为IEnumerable时,如何在asp-for标记帮助器中使用本地化<T> - How to use localization in asp-for tag helper, when the model is an IEnumerable<T> 在循环内将变量绑定到asp-for标签 - Bind a Variable to the asp-for tag inside of loop asp-for 不适用于指定的 ViewComponent 位置 - asp-for is not working with specified ViewComponent locations 指定“asp-for”HTML标记的格式(ASP.NET Core) - Specify a format for “asp-for” HTML Tag (ASP.NET Core) 如何使用 asp-for 标记绑定数组? - How can I bind an array with asp-for tag? 错误的选择标记呈现 id - asp-for 属性 - Wrong select tag rendered id - asp-for property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM