简体   繁体   English

将数据绑定到 ListBox 并将列表发布到数据库 mvc5

[英]Binding data to a ListBox and posting the list to database mvc5

Im developing an mvc5 application with code first approach.我使用代码优先方法开发了一个 mvc5 应用程序。 In one of my views i have a textbox, a add button, a save button, a delete button and a listbox.在我的一个视图中,我有一个文本框、一个添加按钮、一个保存按钮、一个删除按钮和一个列表框。

When i enter a value in textbox(a text) and click add button it should be added to the listbox and show the text.当我在文本框(文本)中输入一个值并单击添加按钮时,它应该被添加到列表框并显示文本。 Hence there'll be multiple text in this listbox.因此,此列表框中将有多个文本。 And also i should be able to delete a record/records in listbox using delete button.而且我应该能够使用删除按钮删除列表框中的一条/多条记录。

After that i want to post this list to server.之后我想将此列表发布到服务器。 Majority of tasks in this scenario can be done using Jquery i suppose.我想这个场景中的大部分任务都可以使用 Jquery 来完成。

upto now what i have done到目前为止我所做的

in _AttributeCreate partial view在 _AttributeCreate 局部视图中

@model eKnittingData.AttributeViewModel

@using (Html.BeginForm("Save", "Attribute"))
{ 
   @Html.TextBox("abc")
   <input type="button" name="AddText" value="Add Text" id="AddText" />

    @Html.ListBoxFor(a => a.typevalue, new SelectList(Model.typevalue, "Id", "Text"))


    <input type="submit" value="Save" id="btn" class="btn btn-success" onclick="disableBtn()" />
}  

script to append items追加项目的脚本

<script>
        $('#AddText').click(function () {
            $('#typevalue').append(
                new Option($('input[name=abc]').val()));
        });  
</script>

AttributeViewModel属性视图模型

public class AttributeViewModel
{
    public IEnumerable<String> typevalue { get; set; }
}

But this is not working.但这是行不通的。 Pls guide me on how to do this(including delete functionality and final post functionailty).请指导我如何执行此操作(包括删除功能和最终发布功能)。 Thanks in advance!提前致谢!

You have a few issues with you code and implementation.您的代码和实现存在一些问题。 @Html.ListBoxFor(a => a.typevalue, new SelectList(Model.typevalue, "Id", "Text")) will not work because typevalue is IEnumerable<String> and string does not contain properties named Id and Text . @Html.ListBoxFor(a => a.typevalue, new SelectList(Model.typevalue, "Id", "Text"))将不起作用,因为typevalueIEnumerable<String>并且string不包含名为IdText属性。 While you could just use @Html.ListBoxFor(a => a.typevalue, null) a listbox is not appropriate from what your trying to do.虽然您可以只使用@Html.ListBoxFor(a => a.typevalue, null)但列表框不适合您尝试做的事情。 A listbox ( <select multiple> element) only posts back the values of its selected options so unless all options are selected when the form is submitted, you will not get the results you expect.列表框( <select multiple>元素)仅回传其选定选项的值,因此除非在提交表单时选择了所有选项,否则您将不会获得预期的结果。

From the comments you are wanting to dynamically add new items to add to the typevalue collection, and have the ability to delete them.从您想要动态添加新项目以添加到typevalue集合的typevalue ,并能够删除它们。 Your html should be something like你的 html 应该是这样的

@using (Html.BeginForm("Save", "Attribute"))
{
  <input type="text" id="newtypevalue" />
  <input type="button" value="Add Text" id="addtypevalue" />
  <div id="typevaluelist">
    // Generate inputs for existing items and in case of returning the view
    foreach(var item in Model.typevalue)
    {
      <div class="typevalue">
        <input type="text" name="typevalue" value="@item" />
        <button type="button" class="delete">Delete</button>
      </div>
    }
  </div>
  ....
}
// Hidden template for adding new items (outside the form element)
<div id="new" style="display:none;">
  <div class="typevalue">
    <input type="text" name="typevalue" />
    <button type="button" class="delete">Delete</button>
  </div>
</div>

and add the following scripts for adding and deleting items并添加以下用于添加和删除项目的脚本

$('#addtypevalue').click(function() {
  var clone = $('#new').clone().children('div'); // copy the template
  clone.find('input').val($('#newtypevalue').val()); // update text
  $('#typevaluelist').append(clone); // add it to the DOM
  $('#newtypevalue').val(''); // clear initial text box
});
$('#typevaluelist').on('click', '.delete', function() {
  $(this).closest('.typevalue').remove(); // remove it from the DOM
});

Refer this fiddle参考这个小提琴

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

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