MVC调查和捕获数据

时间:2014-01-22 21:41:48

标签: asp.net-mvc asp.net-mvc-4

我是MVC的新手,并建立了一个带风险计算器的网站。 一切正常,网站适用于登录的用户,表单计算表单然后将用户发送到正确的页面。

当用户使用帐户登录并填写调查时,我想要捕获数据库上的分数,但无法确定如何/在何处执行此操作。我应该在调查表的后台文件中编写代码吗?

这是脚本

        <script type="text/javascript">

            function test_it(entry) {
                if (entry.value != null && entry.value.length != 0) {
                    entry.value = "" + eval(entry.value);
                }
                computeForm(entry.form);
            }
            function computeForm(form) {
                var total = 0

                for (var count = 0; count < 5; count++) {
                    if (form.a[count].checked) {
                        var total = total + parseInt(form.a[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.b[count].checked) {
                        var total = total + parseInt(form.b[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.c[count].checked) {
                        var total = total + parseInt(form.c[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.d[count].checked) {
                        var total = total + parseInt(form.d[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.e[count].checked) {
                        var total = total + parseInt(form.e[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.f[count].checked) {
                        var total = total + parseInt(form.f[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.g[count].checked) {
                        var total = total + parseInt(form.g[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.h[count].checked) {
                        var total = total + parseInt(form.h[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.i[count].checked) {
                        var total = total + parseInt(form.i[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.j[count].checked) {
                        var total = total + parseInt(form.j[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.k[count].checked) {
                        var total = total + parseInt(form.k[count].value);
                    }
                }

                for (var count = 0; count < 5; count++) {
                    if (form.l[count].checked) {
                        var total = total + parseInt(form.l[count].value);
                    }
                }
                if (total <= 5) { window.location = "End_Page_1" }
                else if (total <= 12) { window.location = "End_Page_2" }
                else if (total <= 24) { window.location = "End_Page_3" }
                else if (total <= 30) { window.location = "End_Page_4" }
                else if (total <= 48) { window.location = "End_Page_5" }
            }

        </script>

任何想法如何我能做到这一点将不胜感激。 安迪

1 个答案:

答案 0 :(得分:0)

假设你有一个模型

public class Calculator
{
public string Field1 {get;set;}
public string Field2 {get;set;}
public string Field3 {get;set;}
...
}

编写一个控制器,它只会向视图发送一个空模型。像

public ActionResult FillSurvey()
{
  Calculator calci =  new Calculator();
  return View(calci);
}

现在右键单击此操作并选择add view此处选择强类型视图。选择您的模型Calculator,然后选择scffold模板。选择创建。将生成一个新视图,您可以在其中输入值并在controller.Like

中再写一个方法/操作
    [HTTPPOST]
     public ActionResult FillSurvey(Calculator calci)
        {
    //do whatever you want here. You will have all the values entered in UI 
//captured in this method
        }