Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
Asp.net core 通过提交和操作将参数传递给控制器_Asp.net Core - Fatal编程技术网

Asp.net core 通过提交和操作将参数传递给控制器

Asp.net core 通过提交和操作将参数传递给控制器,asp.net-core,Asp.net Core,我对asp.net核心比较陌生。 我有一个cshtml视图,如下所示: @model myModel @{ ViewData["Title"] = "Home Page"; } @using(Html.BeginForm("PostIt","Home", FormMethod.Post)) { <input type="hidden" asp-for="@Mo

我对asp.net核心比较陌生。 我有一个cshtml视图,如下所示:

@model myModel

@{
    ViewData["Title"] = "Home Page";
}


@using(Html.BeginForm("PostIt","Home", FormMethod.Post))
{
    <input type="hidden" asp-for="@Model.lmc">
    <input type="hidden" asp-for="@Model.lInt">
    <table>
    @foreach (var item in @Model.lmc)
    {   
        <tr>
            <input type="hidden" asp-for="@item.id">
            <td>@item.name</td>
        </tr>
    }
    </table>
    <input type="submit" value="bySubmit"/>
    <br>
    <a asp-action="byRoute" asp-route-MC="@Model">byRoute</a>
}
@model-myModel
@{
ViewData[“Title”]=“主页”;
}
@使用(Html.BeginForm(“posit”、“Home”、FormMethod.Post))
{
@foreach(@Model.lmc中的var项目)
{   
@item.name
}

拜鲁特 }
我的数据模型如下:

using System.Collections.Generic;

namespace MVCPostList.Models
{
    public class myModel
    {
        public int id{get;set;}
        public List<int> lInt{get;set;}
        public List<myClass> lmc{get;set;}
    }

    public class myClass
    {
        public int id{get;set;}
        public string name{get;set;} 
    }
}
使用System.Collections.Generic;
名称空间MVCPostList.Models
{
公共类myModel
{
公共int id{get;set;}
公共列表lInt{get;set;}
公共列表lmc{get;set;}
}
公共类myClass
{
公共int id{get;set;}
公共字符串名称{get;set;}
}
}
和我的控制器:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MVCPostList.Models;

namespace MVCPostList.Controllers
{
    public class HomeController : Controller
    {
        

        public IActionResult Index()
        {
            myModel mm = new myModel();

            mm.lmc = new List<myClass>();

            myClass mc = new myClass();
            mc.id=1;
            mc.name="test1";
            mm.lmc.Add(mc);

            mc = new myClass();
            mc.id=2;
            mc.name="test2";
            mm.lmc.Add(mc);

            mc = new myClass();
            mc.id=3;
            mc.name="test3";
            mm.lmc.Add(mc);

            return View(mm);
        }

        

        [HttpPost]
        public IActionResult bySubmit(myModel mm)
        {
            return Ok();
        }

        public IActionResult byRoute(myModel mm)
        {
            return Ok();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Logging;
使用MVCPostList.Models;
命名空间MVCPostList.Controllers
{
公共类HomeController:控制器
{
公共IActionResult索引()
{
myModel mm=新的myModel();
mm.lmc=新列表();
myClass mc=新的myClass();
mc.id=1;
mc.name=“test1”;
mm.lmc.Add(mc);
mc=新的myClass();
mc.id=2;
mc.name=“test2”;
mm.lmc.Add(mc);
mc=新的myClass();
mc.id=3;
mc.name=“test3”;
mm.lmc.Add(mc);
返回视图(mm);
}
[HttpPost]
提交的公共IActionResult(myModel mm)
{
返回Ok();
}
公共IActionResult byRoute(myModel mm)
{
返回Ok();
}
}
}
单击bySubmit或byRoute时,myModel.lmc要么为空数组,要么为空数组。 这似乎没有什么帮助,或者我真的不明白它的用途和使用方法。:)
非常感谢您的帮助。谢谢

我会将您视图中的
foreach
循环更改为
for
循环,正如@appect建议的那样:

@for (int i = 0; i < Model.lmc.Count(); i++)
{
    <input type="hidden" asp-for="@Model.lmc[i].id" />
    <td>@Model.lmc[i].name</td>
}
[HttpPost]
public IActionResult bySubmit(myModel mm)
{
    // code to execute
}

[HttpPost]
public IActionResult byRoute(myModel mm)
{
    // code to execute
}
那么您的控制器将如下所示:

@for (int i = 0; i < Model.lmc.Count(); i++)
{
    <input type="hidden" asp-for="@Model.lmc[i].id" />
    <td>@Model.lmc[i].name</td>
}
[HttpPost]
public IActionResult bySubmit(myModel mm)
{
    // code to execute
}

[HttpPost]
public IActionResult byRoute(myModel mm)
{
    // code to execute
}

asp route MC=“@Model”
用于在URL中承载用于路由的值。因此,整个模型无法工作。我上面提供的方法就是我要做的。

很抱歉,BeginForm的帖子已更改为bySubmit…为了帮助诊断此类问题,您可以尝试在浏览器中检查HTML页面,验证表单输入的生成名称。不知何故,它可能意外生成,并且模型将无法正确绑定。此
应生成相应的隐藏输入,其名称类似于
lmc[0]。id
。但我很确定它不会被生成为那种格式,它可能类似于
item.id
。因此,尝试将您的
foreach
循环更改为
for
循环,直接使用
@Model.lmc
而不使用临时变量,例如:
谢谢!!,你的建议对提交人有效,但最终还是失败了。这就足够让我现在走了。再次感谢您使用
asp路由进行的测试
有另一种问题。此
byRoute
不构成提交按钮。您应该改用
。另外,
asp route MC
将指定的数据绑定到
MC
路由键。我没有在您的代码中看到任何
MC
键。表单方法是“post”,因此您的
byRoute
也应该应用
HttpPostAttribute
。我不太确定您想在这里使用
asp-route-
做什么,我认为它不能帮助您以某种隐藏的神奇方式绑定整个
@Model
实例谢谢@gabenotgave。我制作了一个小项目来调查这个问题。当我把它应用到实际的大项目中时,它并没有产生相同的结果。也许我错过了什么…@Mustafa嗯,这很奇怪。。。你的表单和控制器是完全一样的吗?结构上是一样的,但是还有其他的操作发生在那里。实际视图也有部分视图,它们被递归调用以完成页面。我现在想知道这是否会影响it@Mustafa到底是什么不起作用?控制器在提交后是否没有收到任何POST请求?参数在1条件下作为null到达,但在另一条件下作为不完整对象到达。对象中的列表为空(Count==0),而列表中有行用于生成页面。。这个按钮以相同的形式发出咔嗒声。页面的构造方式在不同的条件下有所不同