简体   繁体   English

将angularjs绑定到MVC C#模型数组

[英]Binding angularjs to MVC C# model array

The MVC model contains an array of VCQuestion objects named Questions. MVC模型包含一个名为Questions的VCQuestion对象数组。 (See the definition at the bottom of the code block.) I'm trying to get the following radio button to update the answer field in Question[1]. (请参见代码块底部的定义。)我试图获取以下单选按钮以更新Question [1]中的答案字段。 When the user clicks one of the radio buttons, line 19 in the code block displays the value of the button clicked, but in UpdateOverallRisk(ValuationCheckModel model) in the MVC controller, model does not contain the updated value. 当用户单击单选按钮之一时,代码块中的第19行显示了单击的按钮的值,但是在MVC控制器的UpdateOverallRisk(ValuationCheckModel model)中,模型不包含更新的值。 Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?

<td>{{ valuationcheck.Questions[1].question }}</td>
<td align="center">
    <input type="radio"
           ng-model="valuationcheck.Questions[1].answer"
           ng-change="updateOverallRisk()"
           value="Yes" />
</td>
<td align="center">
    <input type="radio"
           ng-model="valuationcheck.Questions[1].answer"
           ng-change="updateOverallRisk()"
           value="No" />
</td>
<td align="center">
    <input type="radio"
           ng-model="valuationcheck.Questions[1].answer"
           ng-change="updateOverallRisk()"
           value="N/A" />
    <span>{{ valuationcheck.Questions[1].answer }}</span>
</td>

In angularjs controller: 在angularjs控制器中:

$scope.updateOverallRisk = function (model) {
    $http.post('/Products/UpdateOverallRisk', $scope.valuationcheck)
        .then(function (result) {
            if (result.data.Status !== "OK") {
                showErrorAlert("Error handling question");
            }
        });
}

In MVC controller: 在MVC控制器中:

[HttpPost]
public JsonResult UpdateOverallRisk(ValuationCheckModel model)
{
    model.CalculateOverallRisk();
    return Json(new { Status = "OK" });
}


// In MVC model:
public class VCQuestion
{
    public int id;
    public string question;
    public string answer;
    public string riskLevelIfNo;
    public int actualRiskLevel;
}
public VCQuestion[] Questions = new VCQuestion[NUMBER_OF_QUESTIONS];

Paired down version of the MVC model: MVC模型的配对版本:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Comp.Data.Entities.Orders.Products
{
    [Serializable]
    public class ValuationCheckModel
    {
        private const int NUMBER_OF_QUESTIONS = 57;

        public string OverallRisk = "Low";

        public class VCQuestion
        {
            public int id;
            public string question;
            public string answer;
            public string riskLevelIfNo;
            public int actualRiskLevel;
        }

        public int OrderId { get; set; }

        public string ClientName { get; set; }

        public VCQuestion[] Questions = new VCQuestion[NUMBER_OF_QUESTIONS];

        public ValuationCheckModel()
        {
            //Questions = new VCQuestion[NUMBER_OF_QUESTIONS];
            Questions[0] = new VCQuestion()
            {
                id = 1,
                question = "This is question one.",
                answer = "",
                riskLevelIfNo = "Moderate",
                actualRiskLevel = 0
            };
            Questions[1] = new VCQuestion()
            {
                id = 2,
                question = "This is question two.",
                answer = "",
                riskLevelIfNo = "Low",
                actualRiskLevel = 0
            };

            OverallRisk = "Low";
        }

        public void CalculateOverallRisk()
        {
            var total = 0;

            foreach (var question in Questions)
            {
                if (question.answer != "No") continue;
                if (question.riskLevelIfNo == "Moderate")
                    total += 1;
                else if (question.riskLevelIfNo == "High")
                    total += 2;
            }

            if (total > 5)
                OverallRisk = "High";
            else if (total > 2)
                OverallRisk = "Moderate";
            else
                OverallRisk = "Low";
        }

        public static ValuationCheckModel Get(Guid orderGuid)
        {
            ValuationCheckModel model = null;

            model = new ValuationCheckModel();
            model.PopulateWithOrderData(orderId);

            return model;
        }

        private void PopulateWithOrderData(Int32 orderId)
        {
            OrderDetail ordDetail = OrderDetail.Get(orderId).FirstOrDefault();
            if (ordDetail == null) return;
            this.OrderId = orderId;
            this.ClientName = ordDetail.Order.Client.Name;
        }
    }
}

I finally figured it out. 我终于弄明白了。 It was a trivial mistake. 这是一个小错误。 When I created the Question class, I neglected to define the fields with getters and setters. 创建Question类时,我忽略了用getter和setter定义字段。 Once I made that correction, angular had no problem passing the array to MVC. 一旦进行了校正,角度就可以将数组传递给MVC了。 Here's the original class and the corrected version: 这是原始的类和更正的版本:

[Serializable]
public class Question
{
    public int id;
    public string question;
    public string answer;
    public string riskLevelIfNo;
    public int actualRiskLevel;
}

[Serializable]
public class Question
{
    public int id { get; set; }
    public string question { get; set; }
    public string answer { get; set; }
    public string riskLevelIfNo { get; set; }
    public int actualRiskLevel { get; set; }
}

Thanks everybody for your efforts. 感谢大家的努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM