locked
Adding Multiple Records based on the number of clicked checklist items RRS feed

  • Question

  • User927521983 posted

    Hi, I am working of a system that allows for multiple requests.

    So I have View that passes in a hidden Meeting ID and the user can requests multiple support types. But I would like to create a new record for each type of support. Right now the controller adds one entry at a time but I would like to be able to detect the number of types chosen by the user and add the similar number of support request records. See code below. 

    ASP RAZOR VIEW

    @Html.HiddenFor(model => model.MeetingId)

    @foreach(var item in ViewBag.SupportDetails)
    { <input name="SupportDetails" type="checkbox" id="SupportDetails" value="@item.Name" /> @item.Name }

    Controller

    [HttpPost]
    public async Task<ActionResult> Create([Bind(Include = "UniqueId,MeetingId,SupportDetails,CreatedBy,DateCreated,Status")] ConferenceSupportRequest conferenceSupportRequest)
    {
    if (ModelState.IsValid)
    {

    conferenceSupportRequest.Status = "Pending";
    conferenceSupportRequest.DateCreated = DateTime.Now;
    conferenceSupportRequest.CreatedBy = User.Identity.Name;
    db.ConferenceSupportRequests.Add(conferenceSupportRequest);
    await db.SaveChangesAsync();
    return RedirectToAction("Index", "MeetingDetails");
    }}

    Friday, November 2, 2018 8:46 PM

All replies

  • User475983607 posted

    There's a "Learn" link in the menu above that will get you to several getting started tutorials.  The following tutorial illustrates how to design a View, Controller, and View Model to handle multiple checks that are essentially types.

    The start of the tutorial; https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    The actual step; https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

    I would start from the beginning of the tutorial as it builds as it goes but it does show a pattern to use when updating multiple related records.

    Saturday, November 3, 2018 12:48 PM
  • User1520731567 posted

    Hi neil.balram,

    According to your description,it seems that you would like to bulk insert into the database.

    I make a simple demo,you could refer to it:

    View:

    @model List<WebApplication1.Models.Author>
    
    @{
        ViewBag.Title = "Index";
    }
    <h2>Employee List</h2>
    <table class="table">
        <tr>
            <td>
                @Html.TextBox("AuthorName",null, new { @class = "AuthorName" })
            </td>
        </tr>
        <tr>
            <td>
                @Html.TextBox("AuthorName",null, new { @class = "AuthorName" })
            </td>
        </tr>
          <tr>
              <td>
                  @Html.TextBox("AuthorName",null, new { @class = "AuthorName" })
              </td>
                </tr>
        <tr>
            <td colspan="4">
                <input type="submit" value="Submit" onclick="doSave()" />
            </td>
        </tr>
    
    </table>
    
    
    @section scripts{
    <script type="text/javascript">
        function doSave() {
    
            var list = [];
    
            $('table tr').each(function (i, n) {
                var obj = {};
                obj.AuthorName = $(n).find('input[class="AuthorName"]').val();
                list.push(obj);
            });
            $.ajax({
                url: "@Url.Action("BatchInsert_Index", "Authors")",
                type: "post",
                dataType: "json",
                data: { model: list },
                success: function (response) {
                  //....do sth
                }
            });
    
        }
    </script>
        }

    Controller:

     public ActionResult BatchInsert_Index()
            {
                return View();
            }
    
            [HttpPost]
    
            public ActionResult BatchInsert_Index(List<Author> model)
            {
                db.Author.AddRange(model);//use AddRange to save list<> collection 
                db.SaveChanges();
                return View();
    
            }

    How it works:

    Best Regards.

    Yuki Tao

    Tuesday, November 6, 2018 8:27 AM