【问题标题】:adding new values to database from view whose model is a list从模型为列表的视图中向数据库添加新值
【发布时间】:2014-10-23 13:06:35
【问题描述】:

在 ASP.NET-MVC 5 应用程序中,我有以下模型

class Employee {
 int EmployeeID {get;set;}
 string FirstName {get;set;}
 List<OfficeLocations> OfficeLocations {get;set;}
}

class OfficeLocations {
 int OfficeLocationsID {get;set;}
 //foreign key
 int EmployeeID {get;set;}
 string Value1 {get;set;}
 string Value2 {get;set;}
}

我有一个编辑视图,用于修改或添加员工可能所属的不同办公地点。它看起来像这样:

@model List<Project.Models.OfficeLocations>
 @for (int i = 0; i < Model.Count; i++) {
  @Html.EditorFor(m => m[i].CitLocation, new { htmlAttributes = new { @class = "my_editor" } })
  @Html.HiddenFor(m => m[i].OfficeLocationsID)
  @Html.HiddenFor(m => m[i].EmployeeID)
 }
 //extra editor box for adding a new value
 @Html.Editorfor(??????.Value)

对于如何在数据库表中的模型(列表)中添加新条目,我有点困惑。我在额外的 Editorfor 框的参数中输入了什么(所有 ???? 都在哪里)

另外,控制器操作方法应该是什么样子的?

【问题讨论】:

  • 为此,您将需要 javascript/jquery。 This example 可能会帮助您入门
  • 谢谢你的例子,你介意添加一个问题的答案,并添加更多细节,比如 post 方法接受什么作为参数?与最初传递给视图的列表相同吗?
  • 我可以添加一个更详细的答案,但有几件事我不明白。您是否希望您的@model 成为Employee,以便您可以将OfficeLocations 添加到员工,或者只是@model List&lt;OfficeLocations&gt; 在这种情况下您如何传递EmployeeID@Html.EditorFor(m =&gt; m[i].Value1 是什么 - 你没有名为 Value1 的属性 - 你的意思是 m =&gt; m[i].CityLocation 吗?
  • 大声笑好点..实际上我会更新我的代码,但是我需要模型是 List 并且我可以通过执行 m => m[i].EmployeeID 来传递模型
  • 使用反引号。见Comment formatting(点击评论栏右侧的帮助,然后了解更多)

标签: c# asp.net asp.net-mvc entity-framework


【解决方案1】:

将您的视图模型更改为具有办公位置和办公位置列表...这样您可以在额外的编辑器框中添加非列表办公位置对象...或者您可以像这样保留您的视图模型并手动创建一个使用 jquery 建模并使用 ajax jquery 传递它...

【讨论】:

    【解决方案2】:

    为了解决这个问题,我想出了以下 javascript:

    <script>
    
        $(document).ready(function () {
            index = 0;
        });
    
        $('#add_button').click(function () {
    
            var placeHolderNameAttribute = "List2[#].Value1";
            var indexedNameAttribute = "List2[" + index + "].Value1";
    
            var placeHolderIdAttribute = "new_lastName_input";
            var indexedIdAttribute = "new_lastName_input" + index;
    
            document.getElementById(placeHolderIdAttribute).name = indexedNameAttribute;
            document.getElementById(placeHolderIdAttribute).id = indexedIdAttribute;
    
            var clone1 = $("#new_lastName_input" + index).clone();
    
            document.getElementById(indexedIdAttribute).name = placeHolderNameAttribute;
            document.getElementById(indexedIdAttribute).id = placeHolderIdAttribute;
    
            if (index == 0) {
                $('#nlnPlaceHolder').remove();
            }
            $('#LN_editor_box').append(clone1);
            index += 1;
        });
    
    </script>
    

    以及以下占位符输入字段

    <input id="new_lastName_input" class="my_editor" type="text" name="List2[#].Value1" value="New Last Name"  />
    

    现在我的控制器 post 方法接受两个参数,更新/编辑值的原始列表,以及仅包含新值的新列表。

    ActionResult myMethod(List<OfficeLocations> list1, OfficeLocations[] list2)
    

    如果该值在list1中,那么它将在数据库中更新,如果它在list2中,它将被添加

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多