我是 MVC 的新手,很困惑。正在使用具有不同显示页面的两个主要视图,Index 和 DisplayPrediction。我尝试使用数据注释来验证提交表单中的数据,但它只显示错误消息并且不会阻止加载第二个视图。然而,最大的问题是,当第一个视图返回时,输入到文本框中的信息似乎只是简单地重置为默认值。非常感谢您的帮助:)这是我当前的代码:
这是模型,我在这里保留需要从用户那里获取的字段,以及程序的功能(对字段的处理将生成应显示在第二个视图上的输出):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Diagnostics;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.ComponentModel;
namespace MvcHaberman.Models
{
public class HabermanPrediction
{
[Required]
[Range(0,100)]
public int age { set; get; }
[Required]
[Range(58, 69)]
public int year { set; get; }
[Required]
public int axNodes { set; get; }
[Required]
[Range(1, 2, ErrorMessage="Survived = 1; Did not survive = 2")]
public int survival { set; get; }
public string answer { set; get; }
public void generatePrediction()
{
StreamWriter i = new StreamWriter(@"input.arff");
i.WriteLine(@"relation haberman\n\n@attribute Age_of_patient_at_time_of_operation INTEGER\n@attribute Patients_year_of_operation {58,59,60,61,62,63,64,65,66,67,68,69}\n@attribute Number_of_positive_axillary_nodes_detected INTEGER\n@attribute Survival_status {1,2}\n\n@data");
i.WriteLine(age + "," + year + "," + axNodes + "," + survival);
i.Close();
Process genPrediction = new Process();
genPrediction.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
FileName = @"habermanCommand.bat",
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = "cmd.exe"
};
genPrediction.Start();
genPrediction.WaitForExit();
genPrediction.Dispose();
StreamReader readPrediction = new StreamReader(@"output.txt");
string line = readPrediction.ReadLine();
while (line != null)
{
if (line.Contains(':'))
{
string[] part = line.Split(':');
if (part[2].Contains('+'))
{
string[] prediction = part[2].Split('+');
if (part[2][0] == '1')
answer = "\t(Survived)\t\tProbability that your assessment was incorrect:\t" + (Convert.ToDecimal(prediction[1].Trim()) * 100) + "%";
else answer = "\t(Did not Survive)\tProbability that your assessment was incorrect:\t" + (Convert.ToDecimal(prediction[1].Trim()) * 100) + "%";
}
else
{
string line1 = "";
string lineTrimmed = "";
for (int j = 1; j < part[2].Length; j++)
line1 += part[2][j];
for (int l = 0; l < line1.Length; l++)
if (line1[l] != ' ')
lineTrimmed += line1[l];
if (part[2][0] == '1')
answer = "\t(Survived)\t\tProbability that your assessment was correct:\t" + (Convert.ToDecimal(lineTrimmed) * 100) + "%";
else
answer = "\t(Did not Survive)\tProbability that your assessment was correct:\t" + (Convert.ToDecimal(lineTrimmed) * 100) + "%";
}
}
line = readPrediction.ReadLine();
}
readPrediction.Close();
}
}
}
控制器指定数据应在其间流动的两个视图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcHaberman.Models;
using DataAnnotationsExtensions;
namespace MvcHaberman.Controllers
{
public class HabermanController : Controller
{
public ActionResult Index()
{
HabermanPrediction obj = new HabermanPrediction();
if (ModelState.IsValid == false)
{
return Redirect("/Habberman/Index");
}
else return View(obj);
}
public ActionResult About()
{
return View();
}
public ActionResult DisplayPrediction(HabermanPrediction objOut)
{
objOut.generatePrediction();
return View(objOut);
}
}
}
提交视图:
@model MvcHaberman.Models.HabermanPrediction
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>HabermanPrediction</legend>
<div class="editor-label">
@Html.LabelFor(model => model.age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.age)
@Html.ValidationMessageFor(model => model.age)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.year)
</div>
<div class="editor-field" >
@Html.EditorFor(model => model.year)
@Html.ValidationMessageFor(model => model.year)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.axNodes)
</div>
<div class="editor-field" >
@Html.EditorFor(model => model.axNodes)
@Html.ValidationMessageFor(model => model.axNodes)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.survival)
</div>
<div class="editor-field" >
@Html.EditorFor(model => model.survival)
@Html.ValidationMessageFor(model => model.survival)
</div>
<p>
@Html.ActionLink("Generate Prediction", "DisplayPrediction")
</p>
</fieldset>
}
输出视图:
`@model MvcHaberman.Models.HabermanPrediction`
`@using MvcHaberman.Models`
<!DOCTYPE html>
<html>
<head>
<title>DisplayPrediction</title>
</head>
@using (Html.BeginForm())
{
<body>
<div class= "label">
@Html.LabelFor(model => model.answer)
</div>
<div>
<h1>@Model.age</h1>
<h2>@Model.answer</h2>
</div>
</body>
}
</html>