locked
Post & Save multi Item to table using ef 2.1 RRS feed

  • Question

  • User-323149085 posted

    Hello ,

    im trying to select item( using checkbox)  from a  list and post 

    value (EG-Id) to controller

    My end results should be for each id (& more data from the list ) will have its own row in the Db table I would like to send the data . 

    1. I don't know how to post this list to controller from view 

    2 I don't know how to handle this data on the controller side 

      <form asp-action="AddToSellList" class="form-horizontal">
    @foreach (var item in Model) {
    <td>
                @Html.DisplayFor(modelItem => item.ReqText)
            </td>
    (ETC..)
    <td>
                <input type="checkbox" value="@item.Id" name="hidden" />
                   </td>
    
    }
     </tr>
        <div class="col-md-12">
            <input type="submit"  value="Save to list"  class="btn btn-success" />
        </div>
    </form>
    
    
    

      [HttpPost]
    public async Task<IActionResult> AddToSellList( SupSelection sup, int hidden ) { if (ModelState.IsValid) { foreach () //?? { sup.QuettaId=hidden,//... } _context.supSelections.Add()//??; _context.SaveChangesAsync(); }

    Thanks 



     

    Monday, February 11, 2019 12:42 PM

Answers

  • User-821857111 posted

    Your action needs to take a collection type as a parameter, such as a List<int> or int[]:

    public async Task<IActionResult> AddToSellList( SupSelection sup, int[] hidden  )
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2019 2:04 PM

All replies

  • User-821857111 posted

    Your action needs to take a collection type as a parameter, such as a List<int> or int[]:

    public async Task<IActionResult> AddToSellList( SupSelection sup, int[] hidden  )
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2019 2:04 PM
  • User1520731567 posted

    Hi john_mm,

    1. I don't know how to post this list to controller from view 

    According to your code,I suggest you need to avoid the same name,because controller get values from html tags' name.

    So,you could set variable in name,for example:

    @model List<xxx.xxx.SupSelection>

    <form action="@Url.Action("AddToSellList", "Home")" method="POST" class="form-horizontal"> <table> <tr> @for (int i = 0; i < Model.Count(); ++i) { <td> @Html.DisplayFor(modelItem =>Model[i].ReqText) </td> <td> <input type="text" value="@Model[i].QuettaId" name="model[@i].QuettaId" /> </td> } </tr> </table> <div class="col-md-12"> <input type="submit" value="Save to list" class="btn btn-success" /> </div> </form>
    [HttpPost]
            public ActionResult AddToSellList(List<SupSelection> model)
            {
    }

    2 I don't know how to handle this data on the controller side 

    You could refer to this link:

    https://entityframeworkcore.com/saving-data-savechangesasync

    for example:

    public static async Task AddCustomerAsync(string firstName, string lastName, string address)
    {
        using (var context = new MyContext())
        {
            var customer = new Customer
            {
                FirstName = firstName,
                LastName = lastName,
                Address = address
            };
        context.Customers.Add(customer);
        await context.SaveChangesAsync();
    }

    According to your code,I suggest you could move to ASP.NET Core to get more help.

    Best Regards.

    Yuki Tao

    Tuesday, February 12, 2019 6:29 AM
  • User-323149085 posted

    Thanks Mike 

    Tuesday, February 12, 2019 2:52 PM