c# - 如何在验证表单的同时将文件输出到视图?
问题描述
我对 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; }
}
}
看法:
@model WebApplication2.Models.UploadFiles
@{
ViewBag.Title = "Paygroup Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Update Paygroup</h2>
@using (Html.BeginForm("Edit", "UpdateFiles", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(m => m.PayGroup, new {@class = "control-label"})
@Html.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"})
<input type="submit" value="Add" class="btn btn-default"/>
<input type="submit" value="Delete" class="btn btn-default"/>
</div>
}
<table class="table table-striped">
<thead>
<tr>
<th>Paygroups</th>
</tr>
</thead>
<tbody>
<tr>
@foreach (var paygroup in Model.Paygroups)
{
<td>@Model.Paygroups</td>
}
</tbody>
</table>
控制器:
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);
}
}
}
解决方案
对于要在同一页面上显示的单个播放组和整个列表,您可以相应地更改模型,将整个列表存储在一个List<string>
对象中,并将新添加的播放组存储在一个string
对象中。
public PaygroupViewModel
{
public List<string> Paygroups;
public string Paygroup;
}
在该form
部分中,您“生成”一个新的支付组,您需要引用单个Paygroup
属性,这也是在HttpPost
操作方法中传递的参数(而不是整个模型)
在视图中:
@Html.EditorFor(m => m.Paygroup ... )
在控制器中:
[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);
}
然后,在视图中,您可以轻松地在模型列表中循环
@foreach (string paygroup in Model.Paygroups)
{ // build your table }
推荐阅读
- postgresql - 在 postgrSQL 中使用主键
- python-3.x - 可视化链接攻击的最佳方法是什么
- javascript - 减少 React-Native 图像资产大小
- javascript - 使用循环 javascript 创建选择选项
- asp.net-mvc - 将部分视图 select2 控件值传递给父视图控制器
- python - ValueError:时间数据“无”与格式“%Y-%m-%dT%H:%M:%S.%f”不匹配
- html - 每次都强制浏览器链接到新的 pdf 副本?
- r - 将相同的行号分配给R中另一列中的重复值
- android - RecyclerView 中的项目确实保留在 android 中
- javascript - 使用 JQuery 从数组上的单选按钮获取单个值