MVC怀疑视图和控制器之间没有数据传递

时间:2013-04-14 09:34:53

标签: asp.net-mvc data-annotations

我是MVC的新手并且非常困惑。正在使用具有不同显示页面的两个主视图,Index和DisplayPrediction。我尝试使用数据注释来验证提交表单中的数据,但它只显示错误消息,并不会阻止加载第二个视图。但是,最大的问题是输入到文本框中的信息似乎只是在第一个视图返回时重置为默认值。非常感谢帮助:)这是我目前的代码:

这是模型,我保留了需要从用户处获取的字段,以及程序的功能(将生成应显示在第二个视图上的输出的字段上的处理):< / p>

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>

1 个答案:

答案 0 :(得分:0)

使用View(obj)方法时,它会使用名为obj的默认模型生成新的View。所以当页面刷新时,html元素的默认值设置为你的模型参数(在这个场景中obj参数)。