将带有jquery的元素添加到空列表中

时间:2015-11-16 08:58:36

标签: javascript c# jquery model-view-controller

您好我需要将项目添加到列表中,当列表中包含元素但列表为空时 javascript返回undefined

这是我的功能:当列表为空时,namevalue未定义我认为它是主要问题。

function submitAjaxForm(formId) 
{
   var form = $("<form></form>");
   var targetID = formId.substr(1);
   form.append($(formId).html());
   var nameValue = $(formId).find('.showDictionariesList').first().find('input[type="hidden"]').first().attr('name');  
   var indexToCut = nameValue.indexOf('[', nameValue.indexOf('[', 0) + 1);
   var featureIndex = nameValue.substring(0, indexToCut);
   var contentVal = $("#dropDownList option:selected").val(); 
   var returnValue = form.serializeArray();
   returnValue[returnValue.length - 1].value = contentVal;
}

此列表

public List<ShowDictionaryModel> ShowDictionaries { get; set; }

这个项目的类:

public class ShowDictionaryModel
{
   public long Id { get; set; }  
   public string Value { get; set; }  
   public long? ProductId { get; set; } 
   public string FeatureId { get; set; }

   internal static ShowDictionaryModel FromDto(DictionaryDto dto)
   {
      ShowDictionaryModel model = new ShowDictionaryModel();
      model.Id = dto.Id;
      model.Value = dto.Value;
      model.ProductId = dto.ProductId;
      model.FeatureId = dto.FeatureId;
      return model;
   }
}

Cshtml代码:showDictionariesList是包含我想要修改的列表的类

@model MultiShop.Panel.Models.Product.AddProductFeatureTextModel
<div>
        <div id="featureText-@Model.TypeId">

            @Html.HiddenFor(m => m.TypeId)
            @Html.HiddenFor(m => m.IsRequired)
            @Html.HiddenFor(m => m.Name)
            @Html.Label(Model.Name)
            @Html.TextBoxFor(m => m.Value)
            @if (Model.IsRequired)
            { 
                <span class="requiredField"></span>
            }
            @Html.ValidationMessageFor(m => m.Value)
            <div class="showDictionariesList">
                @Html.EditorFor(m => m.ShowDictionaries)
            </div>
            @Html.DropDownListFor(m => m.SelectedDictionaryItemId, new SelectList(Model.DictionaryItems, "Id", "Value", Model.SelectedDictionaryItemId), new { id = "dropDownList" })

            <input type="button" value="Save" onclick="@("submitAjaxForm('#featureText-" + @Model.TypeId + "')")" />
        </div>
        //}

2 个答案:

答案 0 :(得分:1)

您试图获取非现有对象的属性的问题 你应该在检查后得到它

var container = $(formId).find('.showDictionariesList').first();
var nameValueObj = container.find('input[type="hidden"]').first();
if (nameValueObj.length !== 0){
   // input was found so we can get name attribute
   var nameValue= nameValueObj.attr('name');
   var indexToCut = nameValue.indexOf('[', nameValue.indexOf('[', 0) + 1);
   // .........
} else{
   // as far no input elements inside .showDictionariesList your logic to add first one item
   // container.append(
}  

答案 1 :(得分:0)

我得到了答案。我只是使用基本元素初始化列表,如果为null。我在编程方面的经验非常低,所以我的代码看起来很糟糕,但它可以按照我想要的方式工作。

if (!dictionaryType.HasItems())
        {
            DictionaryDto newForEmptyList = new DictionaryDto();
            List<DictionaryDto> newList = new List<DictionaryDto>();
            newForEmptyList.FeatureId = type;
            newForEmptyList.ProductId = productId;
            newForEmptyList.Value = "";
            newForEmptyList.Id = -1;
            newList.Add(newForEmptyList);
            dictionaryType = newList.ToArray();
        }
相关问题