[英]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?
有任何想法吗?
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.