C# 如何通过列表<;元组>;从表到控制器?

C# 如何通过列表<;元组>;从表到控制器?,c#,asp.net-mvc,entity-framework,nullreferenceexception,C#,Asp.net Mvc,Entity Framework,Nullreferenceexception,我有C#中的代码,使用EF Core 1.2,其中有一个包含两个提交按钮的表单。第一个按钮“upload”调用我的方法“UpLoadMyFile”,该方法比较 从我的文本区域到一个模式的每一行,并返回一个字符串,告诉我它是否匹配一个模式。 然后,我将所有文本及其状态添加到 List <Tuple<string, string>> 列表 通过ViewModel传递到视图,并在表格中显示每一行及其状态 现在,当我点击第二个按钮“保存”时,我正试图将每一行保存到我的数据

我有C#中的代码,使用EF Core 1.2,其中有一个包含两个提交按钮的表单。第一个按钮“upload”调用我的方法“UpLoadMyFile”,该方法比较 从我的文本区域到一个模式的每一行,并返回一个字符串,告诉我它是否匹配一个模式。 然后,我将所有文本及其状态添加到

 List <Tuple<string, string>> 
列表
通过ViewModel传递到视图,并在表格中显示每一行及其状态

现在,当我点击第二个按钮“保存”时,我正试图将每一行保存到我的数据库中。但每次我试图保存我的行时,都会发生NullReferenceException 这说明我的表中的列表为空

我想知道如何将表“MyTupleList”中的所有行和状态传递给Post方法,因为我真的不知道如何解决我的问题

我的看法是:

@model Models.ViewModels.CreateSaetzeModelView
<form asp-action="ProcessCreateLines" asp-controller="SoftwareService" method="post" enctype="multipart/form-data">

<div class="div-marginBottom">        
    <table class="table">           
        <tbody>
            <tr>
                <td>
                    <textarea name="ExpressionTextarea" id="ExpressionTextarea" runat="server" TextMode="MultiLine" asp-for="@Model.LoadExpressions"></textarea>

                    <div class="col-md-10">
                        <input type="submit" name="upload" value="upload" /> !--Calls 'uploadmyfile' action-->
                    </div>
                </td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>
<br />
<div class="div-ExpressionEingabe">

</div>
@if (Model.MyLinesList != null)
{
    <div class="div-marginBottom">
        <table id="MyTupleList" class="table table_align">
            <thead>
                <tr>
                    <th>
                        State
                    </th>
                    <th>
                        MyLines
                    </th>
                </tr>
            </thead>
            <tbody>   

                    @foreach (var item in Model.MyLinesList)
                {
                    <tr>
                        <td>                               
                            @Html.DisplayFor(modelItem => item.Item2)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Item1)                            
                    </tr>
                }

            </tbody>
        </table>
        <input type="submit" name="save" value="save" />
    </div>
}
@model Models.ViewModels.CreateSaetzeModelView
!--调用“uploadmyfile”操作-->

@if(Model.MyLinesList!=null) { 陈述 糜棱线 @foreach(Model.MyLinesList中的var项) { @DisplayFor(modelItem=>item.Item2) @DisplayFor(modelItem=>item.Item1) } }

我的代码:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ProcessCreateLines(string upload, string save, CreateLinesModelView cmv)
    {           
        //Button 'upload': Checking my lines 
        if (!string.IsNullOrEmpty(upload))
        {
            string expressions = Request.Form["ExpressionTextarea"].ToString();

            List<Tuple<string, string>> result = new FileUploadController().CheckExpressions(expressions);

            cmv.MyLinesList = result;

            return View("ProcessCreateLines", cmv);

        }

        //Button 'save': Saving my lines into a Database
        if (!string.IsNullOrEmpty(save))
        {
    // ****************************MyLinesList is null*******************
            var list = cmv.MyLinesList;

            ...saving my list into database...
            }

        }
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务流程CreateLines(字符串上载、字符串保存、CreateLinesModelView cmv)
{           
//按钮“上传”:检查我的行
如果(!string.IsNullOrEmpty(上载))
{
字符串表达式=Request.Form[“ExpressionTextarea”].ToString();
列表结果=新建FileUploadController().CheckExpressions(表达式);
cmv.MyLinesList=结果;
返回视图(“ProcessCreateLines”,cmv);
}
//按钮“保存”:将我的行保存到数据库中
如果(!string.IsNullOrEmpty(保存))
{
//**********************************MyLinesList为空*******************
var list=cmv.MyLinesList;
…正在将我的列表保存到数据库中。。。
}
}

多亏@StephenMuecke的评论,我成功地解决了我的问题,使用了一个自制的类而不是Tupel

public class MyListModel
{
    public string myLine { get; set; }
    public string myState { get; set; }
}
}
并在我的ViewModel中创建一个列表

public List<MyListModel> LineWithState { get; set; }
public List LineWithState{get;set;}
同样在我看来,我用for循环替换了foreach循环

@for (int i = 0; i < Model.LineWithState.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.TextBoxFor(m=>m.LineWithState[i].myState)
                        </td>
                        <td>
                            @Html.TextBoxFor(m=>m.LineWithState[i].myLine)
                    </tr>

                }
@for(int i=0;im.LineWithState[i].myState)
@Html.TextBoxFor(m=>m.LineWithState[i].myLine)
}

多亏@StephenMuecke的评论,我成功地解决了我的问题,使用了一个自制的类而不是Tupel

public class MyListModel
{
    public string myLine { get; set; }
    public string myState { get; set; }
}
}
并在我的ViewModel中创建一个列表

public List<MyListModel> LineWithState { get; set; }
public List LineWithState{get;set;}
同样在我看来,我用for循环替换了foreach循环

@for (int i = 0; i < Model.LineWithState.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.TextBoxFor(m=>m.LineWithState[i].myState)
                        </td>
                        <td>
                            @Html.TextBoxFor(m=>m.LineWithState[i].myLine)
                    </tr>

                }
@for(int i=0;im.LineWithState[i].myState)
@Html.TextBoxFor(m=>m.LineWithState[i].myLine)
}

您的视图没有存储
MyLinesList
的内容(它只显示标签,而不是表单数据)。然后单击“保存”按钮时,表单没有要发送到控制器的
MyLinesList
数据,这意味着填充模型时
MyLinesList
属性将为
null
。您的
ProcessCreateLines
方法也违反了SRP,因为您强制两个完全独立的逻辑在同一个方法中工作(没有明显的理由这样做)@Flater我如何在视图中存储内容?例如,我尝试过将@Html.DisplayFor(modelItem=>item.Item2)替换为我不确定如何存储
列表
,我假设一个简单的
HiddenFor
无法自动执行此操作(但无论如何,请尝试一下。如果它有效,您就有了答案)。如果我想办法,我会发布一个答案。您不仅没有创建任何表单控件(因此请求中没有要发送的内容),而且不能使用
元组,因为它没有无参数构造函数(在post方法中无法初始化)并且您不能使用
foreach
-请参阅您的视图没有存储
MyLinesList
的内容(它仅显示标签,而不是表单数据)。然后单击“保存”按钮时,表单没有要发送到控制器的
MyLinesList
数据,这意味着填充模型时
MyLinesList
属性将为
null
。您的
ProcessCreateLines
方法也违反了SRP,因为您强制执行两个完全独立的逻辑为了使用相同的方法(没有明显的理由这样做)@Flater我如何在我的视图中存储内容?我尝试过,例如,将@Html.DisplayFor(modelItem=>item.Item2替换为我不确定如何存储
列表
,我假设