模型绑定angularjs列表

时间:2016-12-31 21:07:48

标签: javascript c# .net angularjs asp.net-mvc

我想在angularJS模型中动态绑定数据 但所有数字数据均为then0 我在其他地方使用这样的代码并且运行良好但现在不能正常工作

  

问题是我的结果是什么

结果

null

我的控制器制作html:

{"id":"73","result":[{"helpState":"","hostRealls":0,"guestsRealls":0,"dateStart":null},{"helpState":"","hostRealls":0,"guestsRealls":0,"dateStart":null},{"helpState":"","hostRealls":0,"guestsRealls":0,"dateStart":null}]

模型

public virtual JsonResult getResult(int id)
    {
        string currentLanguage = LanguageHelpers.GetCurrentLanguageCode;

        var help = db.Helps.AsNoTracking().FirstOrDefault(g => g.id == id && g.id_helpState == 6);

        string helpRow = help?.HelpRow?.HelpRowTitles?.FirstOrDefault(g => g.languageCode == currentLanguage)?.rowContent;

        var data = helpRow?.Split(';');

        ResultHelpViewModel model = new ResultHelpViewModel();
        model.result = new List<onlineResult>();
        var dataResult = help?.onlineResult?.Split(';');

        string html = "<table class=\"table table-striped\">";

        for (int i = 0; i <= help.rowCount; i++)
        {
            onlineResult onlineResult = new onlineResult();
            for (int k = 0; k < (dataResult?.Length ?? 0); k++)
            {
                var dataRowResult = dataResult[k]?.Split('=');
                var dataRowcontentResult = dataRowResult[1]?.Split(',');

                if (dataRowResult[0] == "h")
                    onlineResult.hostRealls = Convert.ToInt32(dataRowcontentResult[i] ?? "0");
                else if (dataRowResult[0] == "g")
                    onlineResult.guestsRealls = Convert.ToInt32(dataRowcontentResult[i] ?? "0");
                else if (dataRowResult[0] == "s")
                    onlineResult.helpState = dataRowcontentResult[i];
                //if (dataRowResult[0] == "s")
                //    onlineResult.dateStart =Convert.ToDateTime(dataRowcontentResult[i]??(DateTime.Now.ToString()));

            }
            model.result.Add(onlineResult);

            html += "<tr>";

            for (int j = 1; j < data.Length - 1; j++)
            {
                var dataRow = data[j].Split('=');
                var dataRowcontent = dataRow[1].Split(',');

                if (i < dataRowcontent.Length && dataRow[0] != "lb" && dataRow[0] != "ch" && !string.IsNullOrEmpty(dataRowcontent[i]))
                    html += "<td>" + dataRowcontent[i] + "</td>";

                if (i != 0)
                {
                    if (j == 2)
                    {
                        html += "<td><input data-ng-model=\"help.result[" + i + "].dateStart\" type=\"text\" class =\"form-control text-center\" value=\"" + model?.result[i]?.dateStart + "\"/></td>";

                        html += "<td><select data-ng-model=\"help.result[" + i + "].helpState\"  class=\"form-control\">";
                        foreach (HelpStateResult result in Enum.GetValues(typeof(HelpStateResult)))
                        {
                            string selected = model?.result[i]?.helpState == ((int)result) + "" ? "selected = \"selected\"" : "";
                            html += "<option label=\"" + Shared_Function.GetEnumDescription(result) + "\" value=\"" + (int)result + "\" " + selected + "></option>";
                        }
                        html += "</select></td>";

                        html += "<td><input data-ng-model=\"help.result[" + i + "].hostRealls\" class =\"form-control text-center\" value=\"" + model?.result[i]?.hostRealls + "\" type=\"number\"/></td>";
                    }
                    if (j == data.Length - 3)
                        html += "<td><input data-ng-init=\"help.result[" + i + "].guestsRealls='" + model?.result[i]?.guestsRealls + "'\" data-ng-model=\"help.result[" + i + "].guestsRealls\"  class =\"form-control text-center\" value=\"" + model?.result[i]?.guestsRealls + "\" type=\"number\"/></td>";
                }
                else if (i == 0 && j == 2)
                    html += "<td>" + Resources.date + "</td>";
                else if (i == 0 && j == 3)
                    html += "<td>" + Resources.help_helpState + "</td>";
                else if (i == 0 && j == 4)
                    html += "<td>" + Resources.help_hostRealls + "</td>";                  
                else if (i == 0 && j == data.Length - 3)
                    html += "<td>" + Resources.help_guestsRealls + "</td>";
            }

            html += "</tr>";
        }
        html += "</table>";

        model.html = html;

        return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

AngularJS控制器

public class ResultHelpViewModel
{
    public long id { get; set; }
    public List<onlineResult> result { get; set; }

    public string html { get; set; }
}

    public class onlineResult
{
    public string helpState { get; set; }

    public int hostRealls { get; set; }

    public int guestsRealls { get; set; }

    public DateTime? dateStart { get; set; }
}

HTML

  $scope.result = function () {          
      $scope.help.id = $stateParams.id;
      $http({
          method: 'POST',
          url: '/Help/Result',
          data: $scope.help,
          headers: {
              'RequestVerificationToken': $scope.antiForgeryToken
          }
      }).success(function (data, status, headers, config) {
          $scope.message = '';
          $scope.errors = [];
          if (data.success === false) {
              $scope.errors = data.errors;
              var str = '';
              for (var error in data.errors) {
                  str += data.errors[error] + '\n';
              }
              $scope.message = str;
          }
          else {
              $state.go('app.listhelps');
          }
      }).error(function (data, status, headers, config) {
          $scope.errors = [];
          $scope.message = 'Unexpected Error new';
      });
  };

1 个答案:

答案 0 :(得分:0)

在这种情况下,最好使用ng-repeat而不是动态地向控制器发送html。