首页 > 解决方案 > 通过提交和操作将参数传递给控制器

问题描述

我对 asp.net core 比较陌生。我有一个像这样的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>
}

我的数据模型如下:

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;} 
    }
}

和我的控制器:

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();
        }
    }
}

单击 bySubmit 或 byRoute 后,myModel.lmc 为空数组或空数组。似乎没有帮助,或者我真的不明白它的用途以及如何使用它。:) 非常感谢任何帮助。谢谢

标签: asp.net-core

解决方案


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

@for (int i = 0; i < Model.lmc.Count(); i++)
{
    <input type="hidden" asp-for="@Model.lmc[i].id" />
    <td>@Model.lmc[i].name</td>
}

关于<a></a>表单中的元素,我会将其设为按钮,然后formaction为两个按钮添加一个属性,以便它知道在提交时要执行哪个操作。

@using(Html.BeginForm("", "Home", FormMethod.Post))
{
    <input type="hidden" asp-for="@Model.lmc" />
    <input type="hidden" asp-for="@Model.lInt" />
    <table>
    @for (int i = 0; i < Model.lmc.Count(); i++)
    {
        <input type="hidden" asp-for="@Model.lmc[i].id" />
        <td>@Model.lmc[i].name</td>
    }
    </table>
    <input type="submit" formaction="bySubmit" />
    <br>
    <input type="submit" formaction="byRoute" />
}

然后您的控制器将如下所示:

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

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

asp-route-MC="@Model"用于在 URL 中携带值以进行路由。因此,整个模型将无法工作。我上面提供的方法就是我会做的。


推荐阅读