我是MVC的新手,很困惑。正在使用不同顯示頁面的兩個主視圖,Index和DisplayPrediction。我嘗試使用數據註釋來驗證提交表單中的數據,但它只顯示錯誤消息,並且不會阻止加載第二個視圖。然而,最大的問題是,當第一個視圖返回時,輸入到文本框中的信息似乎簡單地重置爲默認值。幫助將非常感謝:)這是我現在的代碼:MVC懷疑視圖和控制器之間沒有數據傳遞
這是模型,我保留需要從用戶那裏得到的字段,以及程序的功能(字段上的處理將生成應在所述第二視圖中顯示的輸出):
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\[email protected] Age_of_patient_at_time_of_operation INTEGER\[email protected] Patients_year_of_operation {58,59,60,61,62,63,64,65,66,67,68,69}\[email protected] Number_of_positive_axillary_nodes_detected INTEGER\[email protected] Survival_status {1,2}\n\[email protected]");
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>
請停止將「ASP.NET MVC」簡稱爲「MVC」。一個是框架,而另一個是獨立於語言的設計模式。這就像打電話給IE - 「互聯網」 –
我很高興你意識到我指的是,而不是更少。 – user