如何将模型列表传递给控制器 [英] How to pass model list to controller

查看:120
本文介绍了如何将模型列表传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将视图列表从View发布到控制器。



以下是我的代码:



I am trying to post a list of objects from View to controller.

Below is my code:

@using Models
    @model IList<Add>
    @{
        ViewData["Title"] = "AddNewFields";
    }
    <form asp-controller="Trans" asp-action="InsertFields" method="post" class="form-horizontal card-body" role="form">
<td><input type="text" class="form-control" value="Field Size" asp-for="@Model[i].TypeFlag"/></td>
                            <td><input type="text" class="form-control" value="Field Value" asp-for="@Model[i].FieldValue"/></td>
                            <td><input type="text" class="form-control" value="Field Format" asp-for="@Model[i].FieldFormat"/></td>
</form> 





我会在点击按钮时再次添加这些文本字段。



型号:





I will be adding mo these text fields again on button click.

Model:

public class Add
    {        
        public string TypeFlag { get; set; }

        public string FieldValue { get; set; }

        public string FieldFormat { get; set; }
    }




Controller:

<pre>public string InsertFields(IList<Add> fields)
{
            //some logic
}





当我运行应用程序时,我是得到以下错误:





When I run the application, I am getting the below error:

NullReferenceException: Object reference not set to an instance of an object.

    AspNetCore.Views_Trans_Add.<ExecuteAsync>b__27_0() in AddNewFields.cshtml

            @for (int i = 0; i < Model.Count; i++)

Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)
Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)





我尝试过:





What I have tried:

Please help me...I am stuck here from 1 week..

推荐答案

您似乎没有传递List< add>到视图,所以你得到你所做的错误。您的Razor代码不会检查它是否实际上有一个IList< add>宾语。你只是假设你这样做,你开始尝试从一个甚至不存在的集合中获取元素,因此调用.Count会抛出你所看到的异常。



这是使用调试器追踪的最简单错误。你最好很好地了解调试器,因为它是一个调试你的工具。它就在那里你可以调查和理解你的代码。你首先得到错误的原因是因为你只是猜测你的代码在做什么而不是学习它并理解它。



我无法告诉你为什么Razor代码没有得到一个集合,因为你没有显示构建集合的代码并将其传递给视图。



如果您不知道如何将对象传递给视图,那么您真的需要回过头来学习MVC的基础知识。这是初学者级的东西。

You don't seem to have passed a List<add> to the view, so you get the error that you do. Your Razor code doesn't check to see if it actual got an IList<add> object. You just assume that you do and you start trying to get elements from a collection that doesn't even exist so the call to .Count with throw the exception you're seeing.

This is the easiest error to track down using the debugger. You better get to know the debugger very well because it a tool to debug YOU. It's there so you can investigate and understand your code. The reason you have the error in the first place is because you're just guessing at what you're code is doing instead of learning about it and understanding it.

I can't tell you why the Razor code didn't get a collection because you didn't show the code that builds the collection and passes it to the view.

If you don't know how to pass an object to a view, you REALLY need to go back to learning the basics of MVC. This is beginner level stuff here.
return SomeView(objectToPassToModel);


这篇关于如何将模型列表传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆