список привязки модели angularjs

Я хочу динамически связывать данные в модели angularJS
Но все числовые данные 0или null
Я использую код, как это в другом месте и работал хорошо, но не работает сейчас

Проблема в том, что мой результат-ничто

Результат

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

Мой контроллер сделать html:

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 };
    }

Модель

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; }
}

Контроллер AngularJS

  $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';
      });
  };

формат html

<form class="form-horizontal" role="form" ng-submit='result()' data-ng-init="helpresult()" data-ng-controller="HelpCtrl">
    <input id="antiForgeryToken" data-ng-model="antiForgeryToken" type="hidden" data-ng-init="antiForgeryToken='@ViewBag.GetAntiForgeryToken()'" />
    <p compile data-ng-bind-html="to_trusted(help.html)"></p>
    <hr class="line-dashed line-full" />
    <div class="col-sm-4 col-sm-offset-2">
        <a ui-sref="app.listhelps" class="btn btn-default">@Languages.Properties.Resources.btn_cancel</a>
        <button type="submit" class="btn btn-primary">@Languages.Properties.Resources.btn_submit</button>
    </div>
 </form>

1 ответ

  1. В этом случае лучше использовать ng-repeatвместо отправки html динамически контроллеру.