私はMVCが初めてで、かなり混乱しています。表示ページが異なる 2 つのメイン ビュー、Index と DisplayPrediction が使用されています。データ注釈を使用して送信フォームのデータを検証しようとしましたが、エラー メッセージが表示されるだけで、2 番目のビューの読み込みが妨げられません。ただし、最大の問題は、テキスト ボックスに入力された情報が、最初のビューが戻ったときにデフォルトにリセットされているように見えることです。助けていただければ幸いです:)これが私の現在のコードです:
これがモデルです。ユーザーから取得する必要があるフィールドと、プログラムの機能 (2 番目のビューに表示される出力を生成するフィールドの処理) を保持します。
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();
}
}
}
コントローラーは、データが流れる 2 つのビューを指定します。
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>