Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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 Mvc_Model View Controller - Fatal编程技术网

C# 如何在验证表单的同时将文件输出到视图?

C# 如何在验证表单的同时将文件输出到视图?,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我对MVC应用程序的创建相当陌生,但我有基本的理解。我正在创建一个页面,可以在其中添加和删除文本文件,但视图还包含一个列出文本文件中所有项目的表 到目前为止,我的理解是,我知道如何读取文件并将其输出到列表中,然后可以在视图中的模型和循环中指定该列表,并且我知道如何验证编辑器。然而,我不知道如何同时做到这两个方面 例如: “数据”应包含文本文件中的数据。 对于如何实现这一点的任何解释或示例,我们将不胜感激。我不确定是否需要在模型中指定一个列表,然后通过编辑操作添加到列表中,或者创建一个索引操作并

我对MVC应用程序的创建相当陌生,但我有基本的理解。我正在创建一个页面,可以在其中添加和删除文本文件,但视图还包含一个列出文本文件中所有项目的表

到目前为止,我的理解是,我知道如何读取文件并将其输出到列表中,然后可以在视图中的模型和循环中指定该列表,并且我知道如何验证编辑器。然而,我不知道如何同时做到这两个方面

例如:

“数据”应包含文本文件中的数据。 对于如何实现这一点的任何解释或示例,我们将不胜感激。我不确定是否需要在模型中指定一个列表,然后通过编辑操作添加到列表中,或者创建一个索引操作并在那里执行,或者做什么

型号:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace WebApplication2.Models
{
    public class UploadFiles
    {
        public List<string> Paygroups;

        [Required(ErrorMessage = "Please enter a paygroup.")]
        public string PayGroup { get; set; }
    }
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
命名空间WebApplication2.Models
{
公共类上载文件
{
公开名单付费组;
[必需(ErrorMessage=“请输入薪资组。”)]
公共字符串付款组{get;set;}
}
}
视图:

@model WebApplication2.Models.UploadFiles
@{
ViewBag.Title=“付款组编辑”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
更新薪资组
@使用(Html.BeginForm(“Edit”、“UpdateFiles”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
@LabelFor(m=>m.PayGroup,新的{@class=“control label”})
@EditorFor(m=>m.PayGroup,new{htmlAttributes=new{@class=“form control”,placeholder=Html.DisplayNameFor(m=>m.PayGroup)})
@Html.ValidationMessageFor(m=>m.PayGroup,“,new{@class=“text danger”})
}
工资组
@foreach(Model.Paygroups中的var paygroup)
{
@模型.薪资组
}
控制器:

using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class UpdateFilesController : Controller
    {
        // GET: Default
        public ActionResult Edit()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(string Paygroup)
        {
        if (ModelState.IsValid)
            { 
            var PG = new List<UploadFiles>
            {
                new UploadFiles
                {
                    PayGroup = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/textfiles"), "paygroup.text"))
                }
            };
         }
            return View(model);
        }
    }
}
[HttpPost]
public ActionResult Edit(string Paygroup)
{
    myPaygroupsList = new List<string>();

    // populate the list from file

    // add the new Paygroup to the list
    myPaygroupsList.Add(Paygroup);

    // now create the model:
    PaygroupViewModel model = new PaygroupViewModel() 
    {
        Paygroups = myPaygroupsList
    };
    Return view(model);
}
使用System.Collections.Generic;
使用System.IO;
使用System.Web.Mvc;
使用WebApplication2.Models;
命名空间WebApplication2.Controllers
{
公共类UpdateFileController:控制器
{
//获取:默认值
公共行动结果编辑()
{
返回视图();
}
[HttpPost]
公共操作结果编辑(字符串付款组)
{
if(ModelState.IsValid)
{ 
var PG=新列表
{
新上传文件
{
PayGroup=System.IO.File.ReadAllText(Path.Combine(Server.MapPath(“~/textfiles”),“PayGroup.text”))
}
};
}
返回视图(模型);
}
}
}

对于要在同一页面上显示的单个播放组和整个列表,您可以相应地更改模型,将整个列表存储在
列表
对象中,并将新添加的播放组存储在
字符串
对象中

public PaygroupViewModel
{
    public List<string> Paygroups;
    public string Paygroup;
}
在控制器中:

using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class UpdateFilesController : Controller
    {
        // GET: Default
        public ActionResult Edit()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(string Paygroup)
        {
        if (ModelState.IsValid)
            { 
            var PG = new List<UploadFiles>
            {
                new UploadFiles
                {
                    PayGroup = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/textfiles"), "paygroup.text"))
                }
            };
         }
            return View(model);
        }
    }
}
[HttpPost]
public ActionResult Edit(string Paygroup)
{
    myPaygroupsList = new List<string>();

    // populate the list from file

    // add the new Paygroup to the list
    myPaygroupsList.Add(Paygroup);

    // now create the model:
    PaygroupViewModel model = new PaygroupViewModel() 
    {
        Paygroups = myPaygroupsList
    };
    Return view(model);
}

我已经在我的主要帖子中更新了代码。我有几个问题。。。1.控制器中指定的型号在哪里?我看到您仍在将其发送到视图,但该名称不存在,因为它在HttpPost ActionResult中被替换为字符串Paygroup。是否应该是
字符串付款组、字符串模型
?2.我如何设置我的列表正确吗?3.我是否不再使用ModelState.IsValid?我看到你把它从控制器上取下来了。我不需要它来验证编辑器字段吗?4.我的视图中的循环设置是否正确?我发现
对象引用未设置为对象实例时出错。
1。您必须在编辑操作方法2中创建模型的新实例。我不认为,因为我没有看到您将新的paygroup字符串添加到文件3中。为了简洁起见,我没有添加第4条。您还没有创建模型的实例(请参见第1点),因此这是可以预测的。一旦单击“添加”按钮,新的薪资组将添加到文件中。现在,我正试图集中精力设置列表并在视图中显示它,同时确保编辑器仍然有效,所以我还没有添加代码。我将如何创建模型的新实例?@Xiodrade我已经编辑了我的答案。顺便说一句,这是标准的MVC模式,你应该回顾一下你对它的了解。你是对的。这是标准的。我只是认为它不是为我点击的,因为我在过去通过在控制器的列表中设置模型数据,然后将列表发送到视图,如
返回视图(listname)
对其进行了不同的格式化。尽管创建了模型实例,但我目前仍然会遇到对象错误,但我会自己尝试解决。谢谢你的帮助。
@foreach (string paygroup in Model.Paygroups)
{ // build your table }