如何把两个数组为重点和对方的价值在C#中? [英] How to put two arrays as key and values of each other in C#?

查看:200
本文介绍了如何把两个数组为重点和对方的价值在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.Net MVC形式
其中,触点可以是一个或多个

I have a form in ASP.Net MVC where contacts can be one or many

例如:一个项目可以有一个或多个联系人。
我创造了我的形式,它有两个文本框像吹一个div:

for example: a project can have one or many contact persons. I created my form it it has a div with two textboxes like blow:

<div class="form-group">
        <label class="control-label col-md-2">Focal Points</label>
        <div class="col-md-10">
            <div class="input_fields_wrap">
                <button class="add_field_button btn">Add More Focal Points</button>

                <div style="margin:4px;">
                  Name: <input type="text" name="contact_name[]" />

                  Phone <input type="text" name="contact_phone[]" />
              </div>
            </div>
            </div>
        </div>

当我后我的形式我怎么能获得联系人姓名和电话的数组的键和值的单个阵列。

when I post my form how can I get the array of contact names and phones as a single array of key and values.

脚本,动态生成文本框​​:

script that generates textboxes dynamically:

<script>

$(document).ready(function () {
    var max_fields = 10; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap"); //Fields wrapper
    var add_button = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function (e) { //on add input button click
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div style="margin:4px;">Name: <input type="text" name="contact_name[]"/> Phone <input type="text" name="contact_phone[]" /><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

推荐答案

为了回发到一个模型,控制的名称属性必须名称名称相匹配物业,并为收藏,名称属性必须有一个索引。由于&LT;输入类型=文本名称=CONTACT_NAME []/&GT; 中含有非法的名字,它的价值不能被绑定到模型

In order to post back to a model, the name attribute of the control must match the name name of the property, and for collections, the name attribute must have an indexer. Since <input type="text" name="contact_name[]" /> contains an illegal name, its value cannot be bound to your model.

创建要编辑的属性视图模型

Create a view model with the properties you want to edit

public class ContactVM
{
  public string Name { get; set; }
  public string Phone { get; set; }
}

在你的控制器,初始化要显示任何现有的联系人集合

In your controller, initialize a collection of any existing Contacts you want to display

public ActionResult Edit()
{
  List<ContactVM> model = new List<ContactVM>();
  // add any existing contacts to edit
  return View(model);
}

和视图

@model List<ContactVM>
@using(Html.BeginForm())
{
  <div id="contactlist">
    for(int i = 0; i < Model.Count; i++)
    {
      <div class="contact">
        @Html.TextBoxFor(m => m[i].Name)
        @Html.TextBoxFor(m => m[i].Phone)
        // Add index property to allow dynamic addition and deletion of contacts
        <input type="hidden" name="Index" value="@i" />
        <button type="button" class="deletecontact">Delete</button>
      </div>
    }
  </div>
  <button type="button" id="addcontact">Add More Focal Points</button>
  // Add html for a new contact
  <div id="newcontact"> // style this as hidden
    <div class="contact">
      <input type="text" name="[#].Name value />
      <input type="text" name="[#].Phone value />
      <input type="hidden" name="Index" value ="%"/>
      <button type="button" class="deletecontact">Delete</button>
    </div>
  </div>
  <input type="submit" value="Save" />
}

剧本

$('#addcontact).click(function() {
  var count = $('#contactlist').find('.contact').length;
  if (count < 10)
  {
    var index = (new Date()).getTime(); // unique indexer
    var clone = $('#newcontact').clone();
    clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
    clone.find('input[type="hidden"]').val(index);
    $('#contactlist').append(clone.html());
  } else {
    // cant add any more
  }
});
$('.deletecontact').click(function() {
  $(this).closest('.contact').remove();
}

Post方法

public ActionResult Edit(List<ContactVM> model)
{
  //model now contains all the contacts with the name and phone
}

这篇关于如何把两个数组为重点和对方的价值在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆