I am Unable to fetch Value to textboxes which holds the parent ID

问题内容:

I am trying to accomplish the Updation of my SubHeading clone Form(Child) which comes with the ArticleID(Parent).I am able to show the count of Clone forms under the Article Form while Updating it(Adding and removing of clone form is working under the Article Form ), But, I am unable to get the values in the textboxes of SubHeading while Updating the Form.It is the collection of Data which comes with the Clone form with its Parent ID which is ArticleID.

My Controller:

    public ActionResult EditArticle(int id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      var typeId = 0;
      var catId = 0;
      var subCatId = 0;
      var viewModel = (from sa in ems.SupportArticles
                       join ssc in ems.SupportSubCategories on sa.SubCatID equals ssc.SubCatID
                       join sc in ems.SupportCategories on ssc.CatID equals sc.CatID
                       join st in ems.SupportTypes on sc.TypeID equals st.TypeID
                       where sa.ArcticleId == id
                       select new SupportArticleViewModel { supportArticle = sa, supportSubCat = ssc, supportCat = sc, supportType = st });
      foreach (var vm in viewModel)
      {
        typeId = vm.supportType.TypeID;
        catId = vm.supportCat.CatID;
        subCatId = vm.supportSubCat.SubCatID;
      }

      List<SelectListItem> SupportType = new List<SelectListItem>();
      SupportType.Add(new SelectListItem()
      {
        Text = "Select Support Type",
        Value = "0"
      });
      SupportType.AddRange(ems.SupportTypes.Select(st => new SelectListItem()
      {
        Text = st.Name,
        Value = st.TypeID.ToString(),
        Selected = st.TypeID == typeId
      }));

      List<SelectListItem> SupportCategory = new List<SelectListItem>();
      SupportCategory.Add(new SelectListItem()
      {
        Text = "Select Support Category",
        Value = "0"
      });
      SupportCategory.AddRange(ems.SupportCategories.Select(sc => new SelectListItem()
      {
        Text = sc.CatName,
        Value = sc.CatID.ToString(),
        Selected = sc.CatID == catId
      }));


      List<SelectListItem> SupportSubCategory = new List<SelectListItem>();
      SupportSubCategory.Add(new SelectListItem()
      {
        Text = "Select Support SubCategory",
        Value = "0"
      });
      SupportSubCategory.AddRange(ems.SupportSubCategories.Select(ssc => new SelectListItem()
      {
        Text = ssc.SubCatName,
        Value = ssc.SubCatID.ToString(),
        Selected = ssc.SubCatID == subCatId
      }));

      ViewBag.SupportType = SupportType;
      ViewBag.SupportCategory = SupportCategory;
      ViewBag.SupportSubCategory = SupportSubCategory;
      ViewBag.typeId = typeId;
      ViewBag.catId = catId;
      ViewBag.subcatId = subCatId;

<-- Above: This is Article Updating Part which is parent of its SubHeading-->

      int count = ems.SupportAdds.Where(x=>x.ArticleId == id).Count();
      ViewBag.SCount = count; //This is count of forms comes under the Article while Updating the form.

      for(var i=0; i< ViewBag.SCount;i++)
      {
        SupportAdd sup = new SupportAdd();
        SupportAdd Sup = ems.SupportAdds.Take(1).FirstOrDefault(x => x.ArticleId == id);

        Sup.Title = sup.Title;
        Sup.Description = sup.Description;//Here,I am getting error

      }



      return View(viewModel);
    }

My View:

@model IEnumerable<CMS.Models.SupportArticleViewModel>
<--Clone Forms Parts-->

 @for (var i = 0; i < @ViewBag.SCount; i++)
            {
              if (ViewBag.SCount == 0)
              {
                <h1> No SubHeading</h1>
              }
              else
              {
                <div class="row box">
                   <h1>SubHeading -<span class="icon-prepend input-index-number"> @(i + 1)</span></h1>
                  @Html.LabelFor(model => @item.supportArticleFullViewModel.HeadingTitle)
                  <input type="text" name="HeadingTitle[@(i)]" id="HeadingTitle" class="form-control supportTextBox"  />
                  @Html.LabelFor(m => @item.supportArticleFullViewModel.HeadingImage)
                  <input type="file" name="HeadingImage" id="HeadingImage" class="form-control supportTextBox" />
                  @Html.LabelFor(model => @item.supportArticleFullViewModel.HeadingDescription)
                  <textarea rows="10" class="form-control supportTextBox editor jqte-test" id="ArticleBody" name="HeadingDescription[@(i)]"></textarea>
                  <div class="supportItemCon">
                    <button class="btn btn-no-borders button-remove btn-danger" type="button"> Remove</button>
                  </div>
                  </div>
              }
            }
            <div class="container">
              <div class="row box template">
                  <h1>SubHeading - <span class="icon-prepend input-index-number">1</span></h1>
                  @Html.LabelFor(model => @item.supportdd.Title)
                  <input type="text" name="HeadingTitle[]" id="HeadingTitle" class="form-control supportTextBox" />
                  @Html.LabelFor(m => @item.supportdd.Image)
                  <input type="file" name="HeadingImage" id="HeadingImage" class="form-control supportTextBox" />
                  @Html.LabelFor(model => @item.supportdd.Description)
                  <textarea rows="10" class="form-control supportTextBox editor jqte-test" id="ArticleBody" name="HeadingDescription[]"></textarea>
                  <div class="supportItemCon">
                  <button class="btn btn-no-borders button-remove btn-danger" type="button"> Remove</button>
                    </div>
                </div>
            </div>
            <div class="row">
              <a class="btn btn-success button-add margin-2em-top">&#10010; Add SubHeading</a>
            </div> @*I am not sure with Value.U can modify it *@
<--Script-->
$(document).on("click", ".button-add", function () {
    var lastbox = $('.box').length;
    $('.template').clone()
    .show()
    .removeClass("template")
    .insertBefore('.row:last');
    updateIndexes();
  }).on("click", ".button-remove", function () {
    $(this).closest('.row').remove();
    updateIndexes();
  });

  function updateIndexes() {
    $('.input-index-number:visible').each(function (index) {
      $(this).text(index + 1);
    });
  }

My ViewModel:

  public class SupportArticleViewModel
  {
    public SupportArticle supportArticle { get; set; }

    public SupportAdd supportdd { get; set; }

    public SupportCategory supportCat { get; set; }

    public SupportSubCategory supportSubCat { get; set; }

    public SupportType supportType { get; set; }

    public SupportAdd supportAdd { get; set; }

  }

My Model:

 public class SupportAddViewModel
  {
    public int AddId { get; set; }
    public string Title { get; set; }
    public string Image { get; set; }
    public string Description { get; set; }
    public Nullable<byte> Sort { get; set; }
    public int ArticleId { get; set; }

    public virtual SupportArticle SupportArticle { get; set; }
  }



From this viewModel, I am using Name in SupportAdd Model which is working fine with Add Function
    public class SupportArticleFullViewModel
  {
    public int ArcticleId { get; set; }
    [Required(ErrorMessage = "Your {0} is required")]
    [StringLength(200, MinimumLength = 4, ErrorMessage = "{0} should be between 4 to 200 Char")]
    [DisplayName("Article Title")]
    public string Title { get; set; }
    [DisplayName("Main Image")]
    public string Image { get; set; }
    [DisplayName("Article Body")]
    [AllowHtml]
    [StringLength(int.MaxValue, MinimumLength = 4)]
    [Required(ErrorMessage = "Article Body is required")]
    public string Body { get; set; }
    public System.DateTime Created { get; set; }
    public System.DateTime Updated { get; set; }
    public int SubCatID { get; set; }
    public virtual SupportSubCategory SupportSubCategory { get; set; }
    public HttpPostedFileBase ImageArticle { get; set; }
    [DisplayName("Title")]
    public string[] HeadingTitle { get; set; }
    [DisplayName("Description")]
    public string[] HeadingDescription { get; set; }
    [DisplayName("Image")]
    public string[] HeadingAddImage { get; set; }
    public IList<HttpPostedFileBase> HeadingImage { get; set; }
  }

I am getting Value in Article part, but, I am not getting Value in TextBoxes while Updating the form in Sub-Heading Part.

问题评论:

    
You are creating name attributes which have no relationship to your model. Suggest you look at the answers here and here for dynamically adding and removing collection items
    
I am alomost reached to the destination, But,I am getting one error only
– Siddharth Sinha
6 hours ago

原文地址:

https://stackoverflow.com/questions/47747354/i-am-unable-to-fetch-value-to-textboxes-which-holds-the-parent-id

添加评论