c# - Why is my model and its variables not passing from View to Method inside of my Controller?
问题描述
My application searches for the files that contains a string the user is looking for. So far it does that perfectly. The last thing I need to do is to export it to an excel file so I added a method inside of my controller that is called by the Result
page after I press a button.
The results are stored in a List
of type Result
which is a class with four variables.
The method ExportToExcel
currently returns string so that I can test if the list of results is null. Every single time it has come out as "No Data", therefore it is null. It perfectly prints out a table with the information in the Results
page but does not have the information when i want to export it.
Why is my model not passing from view to method?
At first I wanted to pass my model so that I can access the information in the List
but now I am wondering if it would be better to save the List
data in the controller so that I can directly pass it to my method.
Either way would be fine and I am open to any other ways to do this.
Model
namespace Final.Models
{
public class InputModel:Result
{
public List<Result> Results { get; set; }
}
}
Controller
This controller is just showing how I am passing the InputModel between the views and method. Maybe I am doing something wrong here?
public ActionResult Index()
{
var input = new InputModel();
return View(input);
}
[HttpPost]
public ActionResult Index(InputModel input)
{
//Finds files that contain string.
//send model to Result
return View("Result", input);
}
public ActionResult Result(InputModel input)
{
return View(input);
}
[HttpPost]
public string Result(InputModel input,string export)
{
return ExportToExcel(input);
}
public string ExportToExcel(InputModel input)
{
if (input.Results!=null)
{
//Run excel code here
return "Excel Exported";
}
else
{
return "No Data";
}
}
View for Result
This is part of the view, not the whole thing. I didn't think the full view was necessary but I posted it in the bottom just in case.
@foreach(var result in Model.Results)
{
<tr>
//Return graph of information received
</tr>
}
</table>
<form action="Find/Result" method="POST">
<input type="submit" value="Export" name="export" class="btn btn-default">
</form>
Output
Occurs after pressing the "Export" Button
"No Data"
This is my first MVC applications so once again please let me know if there is any other area I can improve in.
Full View For Result
Changed the form to enclose the entire view as suggested by Wubbly but I get the same output.
@model Final.Models.InputModel
@{
ViewBag.Title = "Result";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<h4>Result</h4>
<hr />
@using (Html.BeginForm("Result", "Find", FormMethod.Post))
{
<p>The <b>@Model.SelectedText</b> files that contain <b>"@Model.Find"</b> are: </p>
<div>
<table class="table table-bordered table-responsive table-hover">
<tr>
//HEADERS
</tr>
@foreach (var result in Model.Results)
{
// int i = 1;
<tr>
<td>@result.SelectedText</td>
<td>@result.FileName</td>
<td>@result.Line</td>
<td>@result.LineCode</td>
</tr>
}
</table>
<div class="form-group">
<div class="col-md-offset-2 ">
<input type="submit" value="Export" name="export" class="btn btn-default">
</div>
</div>
</div>
}
<p>
@Html.ActionLink("Back to List", "Index")
</p>
解决方案
Use for loop with hidden property which takes your property value to model.
@using (Html.BeginForm("Result", "Find", FormMethod.Post))
{
<p>The <b>@Model.SelectedText</b> files that contain <b>"@Model.Find"</b> are: </p>
<div>
<table class="table table-bordered table-responsive table-hover">
<tr>
//HEADERS
</tr>
@for (int i = 0; i < Model.Results.Count; i++)
{
// int i = 1;
<tr>
<td>
@Html.HiddenFor(r => r.Model.Results[i].SelectedText)
@Html.HiddenFor(r => r.Model.Results[i].FileName)
@Html.HiddenFor(r => r.Model.Results[i].Line)
@Html.HiddenFor(r => r.Model.Results[i].LineCode)
@Html.DisplayFor(r => r.Model.Results[i].SelectedText)
</td>
<td>@Html.DisplayFor(r => r.Model.Results[i].FileName)</td>
<td>@Html.DisplayFor(r => r.Model.Results[i].Line)</td>
<td>@Html.DisplayFor(r => r.Model.Results[i].LineCode)</td>
</tr>
}
</table>
<div class="form-group">
<div class="col-md-offset-2 ">
<input type="submit" value="Export" name="export" class="btn btn-default">
</div>
</div>
</div>
}
<p>
@Html.ActionLink("Back to List", "Index")
</p>
推荐阅读
- django - 删除按钮表单标签导致换行
- python - 在 SQLAlchemy 中存在并从 - 中选择 1
- mongodb - 如何使用 Ansible 动态生成 MongoDB 副本集成员列表
- python - 如何在 Matplotlib 中使用数据框生成直方图?
- visual-studio - XUnit 在输出窗口中测试输出,但不在测试资源管理器中,无法调试
- linux - 哪些因素会触发 linux 内核将 IO 缓冲区刷新到磁盘
- r - 同一数据集中的多个 Brown-Forsythe 测试和 paircomp
- android - 在另一个类中引用 MainActivity 的成员
- python - 为什么我的箱线图没有在图中显示清晰的方框?
- c++ - DirectX C++ 游戏的相机