简体   繁体   English

模型绑定angularjs列表

[英]list of model binding angularjs

I want to bind data dynamically in angularJS model But all numeric data are 0 or null I use the code like this in another place and worked well but not working now 我想在angularJS模型中动态绑定数据,但是所有数值数据均为0null我在另一个地方使用了这样的代码,并且效果很好,但现在无法正常工作

The problem is my result is nothing 问题是我的结果什么都没有

Result 结果

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

My controller make html: 我的控制器使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 };
    }

Model 模型

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 Controller 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 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>

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

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

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