如何在此方案中从View中的HTML表格向POST方法发送数据?

问题描述 投票:0回答:1

我有一些数据,我在HTMLView表中显示。现在,我想要做的是点击按钮(比如一个SUBMIT按钮,我想将数据发送到Controller的POST方法,以便我可以将数据保存到数据库中。

我试图使用Model Binding技术获取数据,然而,ViewModel方法中的POST对象出来是null

模型和ViewModel

// Model Model.cs    
public class MyModel
{
    [Key]
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    ...
}

// ViewModel MyViewModel.cs
public class MyViewModel
{
            public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    ...
    public List<MyModel> MyModelList { get; set; }
}

// ViewModel VMList.cs
public class VMList
{
    public List<MyViewModel> MyViewModelList { get; set; }
}

所以,我有一个名为ModelMyModel.cs,它是数据库中的表。然后,我有一个名为ViewModelModelMyViewModel.cs' that has the same columns as theList, in addition to some columns, plus aMyModelofViewModeltype. Then, there is anotherVMList.cscalledMyViewModelthat contains a list of tuples ofViewModeltype. ThisView`的is passed to the

View按以下方式构建:

视图

@model ...Models.ViewModels.VMList
...
<form asp-action="MyAction" asp-controller="MyController" id="myForm">
    <div>
        <button type="submit" value="submit" id="submitButton">Submit</button>
        <table id="myTable">
            @foreach(var item in Model.MyViewModelList)
            {
                <thead>
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Header 3</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            @item.FirstName
                        </td>
                        <td>
                            @item.LastName
                        </td>
                        <td>
                            @item.Header3
                        </td>
                    </tr>
                    @if(item.subList != null && item.subList.Count() != 0)
                    {
                        <thead>
                            <tr>
                                <th>SubList Header 1</th>
                                <th>SubList Header 2</th>
                                <th>SubList Header 3</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach(var subItem in item.subList)
                            {
                                <tr>
                                    <td>
                                        @subItem.SubListHeader1
                                    </td>
                                    <td>
                                        @subItem.SubListHeader2
                                    </td>
                                    <td>
                                        @subItem.SubListHeader3
                                    </td>
                               </tr>
                            }
                        </tbody>
                    }
            }
        </table>
    </div>
</form>    

POST方法

[HttpPost]
public IActionResult MyAction(VMList vmListObject)
{
    return View();
}

如何将View表格中显示的数据恢复为Controller

c# asp.net-mvc asp.net-core-mvc viewmodel
1个回答
2
投票

根据您给出的模型,尝试使用asp-for tag helper和name属性进行模型绑定。

@model  VMList
<form asp-action="MyAction" asp-controller="HomeController" id="myForm">
<div>
    <button type="submit" value="submit" id="submitButton">Submit</button>
    <table id="myTable">
        @{ int i = 0;}
        @foreach (var item in Model.MyViewModelList)
        {
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Header 3</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <input asp-for="@item.FirstName" name="vmListObject.MyViewModelList[@i].FirstName" />

                    </td>
                    <td>
                        <input asp-for="@item.LastName" name="vmListObject.MyViewModelList[@i].LastName"/>
                    </td>
                    <td>
                        <input asp-for="@item.Header3" name="vmListObject.MyViewModelList[@i].Header3" />
                    </td>
                </tr>
                @if (item.MyModelList != null && item.MyModelList.Count() != 0)
                {
                    <thead>
                        <tr>
                            <th>SubList Header 1</th>
                            <th>SubList Header 2</th>
                            <th>SubList Header 3</th>
                        </tr>
                    </thead>
                    <tbody>
                        @{ int j = 0;}
                        @foreach (var subItem in item.MyModelList)
                        {
                            <tr>
                                <td>
                                    <input asp-for="@subItem.FirstName" name="vmListObject.MyViewModelList[@i].MyModelList[@j].FirstName"/>
                                </td>
                                <td>
                                    <input asp-for="@subItem.LastName" name="vmListObject.MyViewModelList[@i].MyModelList[@j].LastName" />
                                </td>
                                <td>
                                    test
                                </td>
                            </tr>
                            j++;
                        }

                    </tbody>
                  }
            </tbody>
           i++;
         }
    </table>
</div>
</form>
© www.soinside.com 2019 - 2024. All rights reserved.