I have this code in C# using EF Core 1.2 where I have a form containing two submit buttons. The first button 'upload' invokes my method 'UpLoadMyFile' which compares each line from my textarea to a pattern and returns a string which tells me if it matches one pattern. Then I add all my text and its states into a
我在使用EF Core 1.2的C#中有这个代码,我有一个包含两个提交按钮的表单。第一个按钮'upload'调用我的方法'UpLoadMyFile',它将我的textarea中的每一行与一个模式进行比较,并返回一个字符串,告诉我它是否与一个模式匹配。然后我将所有文本及其状态添加到一个
List <Tuple<string, string>>
that I pass to my View via a ViewModel and display each line plus its state in a table.
我通过ViewModel传递给我的View并在表格中显示每一行及其状态。
Now I'm trying to save each line into my database when I click my second button 'save'. But every time I try to save my lines a NullReferenceException occurs which tells me my List from my table is null.
现在,当我点击第二个按钮“保存”时,我正试图将每一行保存到我的数据库中。但是每次我尝试保存我的行时都会发生NullReferenceException,告诉我我的表中的List是null。
I would like to know how to pass all lines and states from my Table 'MyTupleList' to my Post Method since I really don't know how to fix my problem.
我想知道如何将我的表'MyTupleList'中的所有行和状态传递给我的Post方法,因为我真的不知道如何解决我的问题。
My View:
@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>
}
My Code:
[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...
}
}
1 个解决方案
#1
0
I managed to solve my problem thanks to the comment of @StephenMuecke by using instead of a Tupel a selfmade class
由于@StephenMuecke的评论使用而不是使用Tupel自制类,我设法解决了我的问题
public class MyListModel
{
public string myLine { get; set; }
public string myState { get; set; }
}
}
and creating a List out of it in my ViewModel
并在我的ViewModel中创建一个List
public List<MyListModel> LineWithState { get; set; }
Also in my View I replaced the foreach loop with a for loop
同样在我的视图中,我用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>
}
#1
0
I managed to solve my problem thanks to the comment of @StephenMuecke by using instead of a Tupel a selfmade class
由于@StephenMuecke的评论使用而不是使用Tupel自制类,我设法解决了我的问题
public class MyListModel
{
public string myLine { get; set; }
public string myState { get; set; }
}
}
and creating a List out of it in my ViewModel
并在我的ViewModel中创建一个List
public List<MyListModel> LineWithState { get; set; }
Also in my View I replaced the foreach loop with a for loop
同样在我的视图中,我用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>
}