Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的模型及其变量没有在我的控制器中从视图传递到方法?_C#_Asp.net_Asp.net Mvc_Model_Controller - Fatal编程技术网

C# 为什么我的模型及其变量没有在我的控制器中从视图传递到方法?

C# 为什么我的模型及其变量没有在我的控制器中从视图传递到方法?,c#,asp.net,asp.net-mvc,model,controller,C#,Asp.net,Asp.net Mvc,Model,Controller,我的应用程序搜索包含用户正在查找的字符串的文件。到目前为止,它做到了完美。我需要做的最后一件事是将其导出到excel文件,因此我在控制器中添加了一个方法,按下按钮后,Result页面将调用该方法 结果存储在类型为Result的列表中,该类包含四个变量 方法ExportToExcel当前返回字符串,以便我可以测试结果列表是否为空。每一次它都显示为“无数据”,因此它是空的。它完美地打印出一个表,其中包含结果页面中的信息,但当我想要导出它时,它没有这些信息 为什么我的模型没有从视图传递到方法 起初我想

我的应用程序搜索包含用户正在查找的字符串的文件。到目前为止,它做到了完美。我需要做的最后一件事是将其导出到excel文件,因此我在控制器中添加了一个方法,按下按钮后,
Result
页面将调用该方法

结果存储在类型为
Result
列表中,该类包含四个变量

方法
ExportToExcel
当前返回字符串,以便我可以测试结果列表是否为空。每一次它都显示为“无数据”,因此它是空的。它完美地打印出一个表,其中包含
结果
页面中的信息,但当我想要导出它时,它没有这些信息

为什么我的模型没有从视图传递到方法

起初我想传递我的模型,这样我就可以访问
列表中的信息
,但现在我想知道是否最好将
列表
数据保存在控制器中,以便直接传递给我的方法

任何一种方法都可以,我愿意接受任何其他方法

型号

namespace Final.Models
{
    public class InputModel:Result
    {

        public List<Result> Results { get; set; }

    }  
}
查看结果

这是观点的一部分,而不是全部。我不认为完整的视图是必要的,但我把它贴在底部以防万一

 @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>
这是我的第一个MVC应用程序,请再次告诉我是否还有其他方面可以改进

结果的完整视图

按照Wubbly的建议,将表单更改为包含整个视图,但我得到了相同的输出

@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>
@model Final.Models.InputModel
@{
ViewBag.Title=“结果”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}

结果
@使用(Html.BeginForm(“Result”、“Find”、FormMethod.Post)) { 包含“@Model.Find”的@Model.SelectedText文件包括:

//标题 @foreach(Model.Results中的var结果) { //int i=1; @result.SelectedText @result.FileName @结果线 @result.LineCode } } @ActionLink(“返回列表”、“索引”)


使用带有隐藏属性的for循环,它将属性值带到模型中

@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>
@使用(Html.BeginForm(“Result”、“Find”、FormMethod.Post))
{
包含“@Model.Find”的@Model.SelectedText文件包括:

//标题 @对于(int i=0;ir.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) @DisplayFor(r=>r.Model.Results[i].SelectedText) @DisplayFor(r=>r.Model.Results[i].FileName) @DisplayFor(r=>r.Model.Results[i].Line) @DisplayFor(r=>r.Model.Results[i].LineCode) } } @ActionLink(“返回列表”、“索引”)


对于任何可能需要答案且处于类似情况的人,我都解决了我的问题。你们中的许多人可能认为这不是解决问题的正确方法,但这对我来说是有效的。无论哪种方式,只要有任何反馈,我都将不胜感激,以提高我的能力

首先,我确实按照StephenMuecke和ShailendraKumar的建议,将我的
foreach
更改为
for
循环

我将数据从我的
HTTPGet
传输到我的
HTTPPost
的方式是使用
TempData
。我使用它将用户输入的模型存储在我的
HTTPPost
索引中,并在我的
HTTPPost
结果中调用它

下面是我如何更换控制器的

       public ActionResult Index()
        {
            var input = new InputModel();
            input.Type = input.FillType(input.Type);

            return View(input);
        }

        [HttpPost]
        public ActionResult Index(InputModel input)
        {
            input.FileType = input.ValueConvert();
            input.FileFind();
            TempData["model"] = input
            return View("Result", input);
        }

        public ActionResult Result(InputModel input)
        {
            return View(input);
        }


        [HttpPost]
        public void Result()
        {
            InputModel model = new InputModel();
            model = (InputModel)TempData["model"];
            model.ExportToExcel();
        }

看起来您提交的是一个空表单,因为您可以看到表单只包含一个按钮,而没有其他内容,这就是为什么
输入.Results
为空。如果您提交的表单为空,为什么您希望填充InputModel?这是您的完整视图吗?您是否已将所有内容复制到问题中?您仍然没有提交任何表单元素。在学习ASP.NET MVC之前,您可能应该先学习HTML表单的基础知识,以及它们如何将数据发布到web服务器。您的表单没有表单控件,因此无需提交任何内容。如果包含表单控件,则不能使用
foreach
循环(请参阅。但这有什么意义呢?您没有编辑任何内容,那么您究竟为什么要将刚刚从控制器发送的所有数据发送回控制器?这只是毫无意义的额外开销,会降低性能。如果您需要数据,那么您可以通过调用数据库在控制器中再次获取数据。
@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>
       public ActionResult Index()
        {
            var input = new InputModel();
            input.Type = input.FillType(input.Type);

            return View(input);
        }

        [HttpPost]
        public ActionResult Index(InputModel input)
        {
            input.FileType = input.ValueConvert();
            input.FileFind();
            TempData["model"] = input
            return View("Result", input);
        }

        public ActionResult Result(InputModel input)
        {
            return View(input);
        }


        [HttpPost]
        public void Result()
        {
            InputModel model = new InputModel();
            model = (InputModel)TempData["model"];
            model.ExportToExcel();
        }