【发布时间】:2020-09-09 08:44:29
【问题描述】:
我正在使用 MVC .net 框架,我对 asp.net mvc 很陌生,提前道歉。
我的查询是我能够在我的第一个控制器中成功加载一个 csv 文件并查看。然后从我的第一个视图转到我的NextController,然后它会进行主要处理。到这里一切都好。从这里我现在必须将其移至下一个控制器的视图FxProcess。相反,它返回到现有视图并抱怨新模型FxOrder 的命名空间问题。我哪里错了?这是我的控制器和视图:
HomeController.cs
public class HomeController : Controller
{
// GET: Home
public ActionResult Upload()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload(HttpPostedFileBase upload, SecTypeDetails secTypeUsed)
{
if (ModelState.IsValid)
{
if (upload != null && upload.ContentLength > 0)
{
if (upload.FileName.EndsWith(".csv"))
{
var csvParser = new TextFieldParser(upload.InputStream);
var fileName = upload.FileName;
var newview = new Trade0Tron.web.Models.SecTypeDetails { sectype = secTypeUsed.sectype, csvParser = csvParser, fileName = fileName };
return View(newview);
}
else
{
ModelState.AddModelError("File", "This file format is not supported");
return View();
}
}
else
{
ModelState.AddModelError("File", "Please Upload Your file");
}
}
return View();
}
}
上传.cshtml
@model Trade0Tron.web.Models.SecTypeDetails
@using System.Data;
<h2>Upload File</h2>
@using (Html.BeginForm("Upload", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="file" id="dataFile" name="upload" />
</div>
<div class="form-group">
<input type="submit" value="Upload" class="btn btn-default" />
</div>
</div>
<div class="col-md-8">
<div class="form-group">
Select SecType to run
</div>
<div class="form-group">
@Html.EnumDropDownListFor(
x => x.sectype,
new { @class = "form-control" })
</div>
</div>
</div>
if (Model != null)
{
@Html.Action("FxProcess", "Next", Model)
}
}
NextController.cs
public class NextController : Controller
{
public ActionResult FxProcess(SecTypeDetails secTypeUsed)
{
var wk = new Work();
var fxOrder = wk.FxFileProcessing("", secTypeUsed.fileName, "csv", secTypeUsed.sectype.ToString(), false, secTypeUsed.csvParser);
return View("FxProcess", fxOrder);
}
}
对应的cshtml如下
@model Models.Fx.FxOrder
@using System.Data;
<h2>Upload File</h2>
@using (Html.BeginForm("FxProcess", "FxTrade"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
if (Model != null)
{
@Html.Action("GetResult", "FxResult", Model)
理想情况下,我希望NextController 操作的结果转到其相应的视图,然后转到结果控制器。这又有一个结果视图来显示结果。另请注意,NextController 有一个新模型 Models.Fx.FxOrder。
【问题讨论】:
-
很难理解。
return View("FxProcess", fxOrder);不会去 FxProcess.cshtml 吗?有用吗?:stackoverflow.com/questions/33634897/…
标签: c# asp.net asp.net-mvc model-view-controller