ASP.NET MVC:如何将多个“EditorFor”插入数据库?

时间:2015-12-03 17:09:22

标签: asp.net asp.net-mvc asp.net-mvc-4

assigns a value

如果单击“+”,将显示带有“x”的新文本框。如果我单击“x”,将删除该同义词行。我现在的问题是如何将所有这些同义词保存到数据库中。所以数据库应该如下所示:

    public ActionResult Create()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Create(NPG_ChemicalViewModel model)
    {
        using (var context = new NPG_Model())
        {
            var chemical = new NPG_Chemical();

            chemical.CAS_Number = model.NPG_Chemical.CAS_Number;
            context.NPG_Chemical.Add(chemical);
            context.SaveChanges();

            var synonym = new NPG_Chemical_Synonym();
            synonym.Synonym = model.NPG_Chemical_Synonym.Synonym;
            context.NPG_Chemical_Synonym.Add(synonym);
            context.SaveChanges();

            var initialData = new[] {
             new NPG_Chemical_Synonym { Synonym = "" },
            };
        }
    return View();
    }

    public ViewResult BlankEditorRow()
    {
        return View("GiftEditorRow", new NPG_ChemicalViewModel());
    }

现在它只保存第一个。 从此示例enter image description here开始。它让我使用“BeginCollectionItem”,但它说我的模型不包含“BeginCollectionItem”的定义。

任何人都可以帮我解决这个问题吗?或者还有其他方法吗?

这是化学控制器:

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4 align="center">Add Chemical</h4>
    <hr />
    <table style="width:100%;">
        <tr>
            <td>Synonym</td>
            <td>
                <div id="editorRows">
                        @{Html.RenderPartial("GiftEditorRow");}
                </div>
                <div>
                    @Html.ActionLink("+", "BlankEditorRow", null, new { id = "addItem" })
                </div>
            </td>
        </tr>
        <tr>
        <tr>
            <td>CAS No.</td>
            <td>
                <div>
                    @Html.EditorFor(model => model.NPG_Chemical.CAS_Number, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.NPG_Chemical.CAS_Number, "", new { @class = "text-danger" })
                </div>
            </td>
        </tr>
    </table>
   <script>
       $("#addItem").click(function () {
           $.ajax({
               url: this.href,
               cache: false,
               success: function (html) { $("#editorRows").append(html); }
           });
           return false;
       });
       </script>
</div>
<div class="form-group">
    <div class="col-md-offset-3 col-md-9">
        <input type="submit" value="Submit" class="btn btn-default" />
    </div>
</div>
}

这是create.cshtml:

<div class="editorRow">
<a href="#" class="deleteRow">x</a>
@Html.EditorFor(x => x.NPG_Chemical_Synonym.Synonym)
</div>
<script>
$(document).on('click', 'a.deleteRow', function () {
    $(this).parents("div.editorRow:first").remove();
    return false;
});
</script>

这是BlankEditorRow.cshtml:

namespace NPG_Administrative_Utility.Models
{
public class NPG_ChemicalViewModel
{
    public NPG_ChemicalViewModel()
    {
        NPG_Chemical = new NPG_Chemical();
        NPG_Chemical_Synonym = new NPG_Chemical_Synonym();
    }
    public NPG_Chemical NPG_Chemical { get; set; }
    public NPG_Chemical_Synonym NPG_Chemical_Synonym { get; set; }
}
}

这是NPG_ChemicalViewModel:

namespace NPG_Administrative_Utility.Models
{
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public partial class NPG_Chemical_Synonym
{
    [Key]
    [Column(TypeName = "numeric")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public decimal NPG_Chemical_Synonym_ID { get; set; }

    [Column(TypeName = "numeric")]
    public decimal NPG_Chemical_ID { get; set; }

    [StringLength(512)]
    public string Synonym { get; set; }
}
}

这是同义词型号:

{{1}}

1 个答案:

答案 0 :(得分:-1)

<div ng-form="setPlayersForm" ng-repeat="player in rp.KillerService.getPlayers() track by $index" class="name-input"> <input type="text" name="playerName" placeholder="Your Name" ng-model="player.name" required/> <a class="fa fa-trash-o" ng-if="player.newPlayer" ng-click="rp.removePlayer(player)"></a> <ng-messages for="setPlayersForm.playerName.$error" ng-if="rp.submitted"> <ng-message when="required" class="alert-box alert">This field is required</ng-message> </ng-messages> </div> 添加到您的模型中并使用视图中的每个循环。 你可以在视图中拥有尽可能多的ICollection<Synonym>,你可以通过jQuery隐藏/显示它们。模型绑定将为您找到所有EditorFor!你可以在你的控制器中处理它。