Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表<;字符串>;以System.Collections.Generic.List的形式返回HttpPost wiht MVC_C#_List_Asp.net Mvc 5 - Fatal编程技术网

C# 列表<;字符串>;以System.Collections.Generic.List的形式返回HttpPost wiht MVC

C# 列表<;字符串>;以System.Collections.Generic.List的形式返回HttpPost wiht MVC,c#,list,asp.net-mvc-5,C#,List,Asp.net Mvc 5,我有一个用MVC5编写的Web应用程序,它将一个对象列表(包含许多项)传递到一个页面并成功呈现。我在表单上有一个按钮,可以强制返回帖子。当我点击按钮时,模型会重新初始化对象列表,而不是返回页面上的内容 我已经阅读了很多文章,涵盖了类似的问题,并提出了很多建议,比如确保对象中的每一项都在表单上(至少是隐藏的)。尝试了许多选择,但到目前为止还没有成功地解决我的问题 我决定回到它的基础上,创建了一个带有列表的非常简单的视图模型。这将再次呈现ok,但返回时为System.Collections.Gene

我有一个用MVC5编写的Web应用程序,它将一个对象列表(包含许多项)传递到一个页面并成功呈现。我在表单上有一个按钮,可以强制返回帖子。当我点击按钮时,模型会重新初始化对象列表,而不是返回页面上的内容

我已经阅读了很多文章,涵盖了类似的问题,并提出了很多建议,比如确保对象中的每一项都在表单上(至少是隐藏的)。尝试了许多选择,但到目前为止还没有成功地解决我的问题

我决定回到它的基础上,创建了一个带有列表的非常简单的视图模型。这将再次呈现ok,但返回时为System.Collections.Generic.List

查看模型

public class TestVm
{
    public List<string> CustomerNames { get; set; }
}
公共类TestVm
{
公共列表客户名称{get;set;}
}
控制器

public ActionResult Index()
{
    TestVm testmodel = new TestVm();

    testmodel.CustomerNames = new List<string>();
    testmodel.CustomerNames.Add("HELP");
    testmodel.CustomerNames.Add("Its");
    testmodel.CustomerNames.Add("Not");
    testmodel.CustomerNames.Add("Working");
    return View(testmodel); 
}

[HttpPost]
public ActionResult Index(TestVm model)
{
    // DO SOME WORK HERE WITH RETURNED model
    return View(model);
}
public ActionResult Index()
{
TestVm testmodel=newtestvm();
testmodel.CustomerNames=新列表();
testmodel.CustomerNames.Add(“帮助”);
testmodel.CustomerNames.Add(“Its”);
testmodel.CustomerNames.Add(“非”);
testmodel.CustomerNames.Add(“工作”);
返回视图(testmodel);
}
[HttpPost]
公共操作结果索引(TestVm模型)
{
//在这里使用返回的模型做一些工作
返回视图(模型);
}
查看

@model WebApplication1.Models.TestVm
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View</title>
</head>
<body>

@using (Html.BeginForm("Index", "Test", Model, FormMethod.Post, new { @class = "form-horizontal" }))
{
    <button name="submit" type="submit" value="Refresh" class="btn btn-sm btn-default pull-right">Refresh</button>
}
<div>
@if (Model.CustomerNames != null)
{

<table>
    <thead>
    <tr>
        <td class="text-center">CustomerName</td>
    </tr>
    </thead>
    <tbody>
    @for (int i = 0; i < Model.CustomerNames.Count(); i++)
    {
        <tr>
            <td class="text-center">@Html.EditorFor(m => m.CustomerNames[i])</td>
            </tr>
        }
        </tbody>
        <tfoot>
        </tfoot>
    </table>
}
</div>
</body>
</html>
@model WebApplication1.Models.TestVm
@{
布局=空;
}
看法
@使用(Html.BeginForm(“Index”,“Test”,Model,FormMethod.Post,new{@class=“form horizontal”}))
{
刷新
}
@if(Model.CustomerNames!=null)
{
客户名称
@对于(int i=0;im.CustomerNames[i])
}
}
我认为创建这样一个简单的应用程序可以帮助我理解并解决我的问题。但是我不明白为什么HttpPost中的模型包含“System.Collections.Generic.List”,而不是我所期望的字符串的实际列表

初始加载

刷新后


您需要将每个表单控件放在
Html.BeginForm
括号中

@using (Html.BeginForm("Index", "Test", Model, FormMethod.Post, new { @class = "form-horizontal" }))
{
    <button name="submit" type="submit" value="Refresh" class="btn btn-sm btn-default pull-right">Refresh</button>

    <div>
    @if (Model.CustomerNames != null)
    {

        <table>
            <thead>
                <tr>
                    <td class="text-center">CustomerName</td>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < Model.CustomerNames.Count(); i++)
                {
                    <tr>
                        <td class="text-center">@Html.EditorFor(m => m.CustomerNames[i])</td>
                    </tr>
                }
            </tbody>
            <tfoot>
            </tfoot>
        </table>
    }
    </div>
}
@使用(Html.BeginForm(“Index”,“Test”,Model,FormMethod.Post,new{@class=“form horizontal”}))
{
刷新
@if(Model.CustomerNames!=null)
{
客户名称
@对于(int i=0;im.CustomerNames[i])
}
}
}

因为您的表单不包含任何表单控件,noting将提交到POST方法,列表为空。至于为什么它会显示
System.Collections.Generic.List
-这是因为您没有显示代码(
//在这里对返回的模型做一些工作
)-显示相关代码谢谢@StephenMuecke的回复。当时没有相关的代码,因为我仍然需要在该区域编写代码。正如DPac在下面建议的那样,返回的GenericList完全是我在“@Using”语句的右括号中的位置。是的,我知道(在我之前的评论的第一行),但这并不能解释为什么返回视图时会得到
System.Collections.Generic.List
(这可能是由于您没有显示的代码的另一个问题)感谢@DPac的响应。将我的代码更改为将标记中的所有内容都包含在“@using{..}”中,但它实际上仍然返回相同的结果。只需在我的主应用程序上再次尝试,它就可以工作。必须移动结束括号。