Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Hi A bit help with MVC, thank you

How to submit values from Form with Enumerable<project.model.classA> into a class B ? 
I displayed values from Entity A with groupby (Name) command 
I changed the DisplayFor control for a EditFor control to set values and it will allow the system to submit them. //that's what I think.

class CallRecord 
- I got his values by uploading a file and inserting those values into each matched column on this class
+-------+------+--------------+
|Id     | Name | Cost         |
+-------+------+--------------+
|   1   | Anna |   30.3       |
+-------+------+--------------+
|   2   | Anna |   500        |
+-------+------+--------------+
|   3   | Sam  |   30.3       |
+-------+------+--------------+
|   4   | Tom  |   20.5       |
+-------+------+--------------+
|   5   | Tom  |   20.5       |
+-------+------+--------------+
|   6   | Anna |   20.5       |
+-------+------+--------------+


Grouped result values for class CallRecord: Grupdby Name

Name  Cost
Anna  550.8  
Sam    30.3
Tom    41

but I added a selectList for each displayed grouped result

Name  Cost      Place
Anna  550.8  select place = Place is a list added to each displayed value
Sam    30.3  select place = Place is a list added to each displayed value
Tom    41    select place = Place is a list added to each displayed value

after user select Place value for each displayed on list user is allow to save

save = if sumbit it pass all values to class RegisterCall

class Place

 Id
Name

class RegisterCall

Id
Fname
Place


Desire result after submited from Index 
with a @model IEnumerable<project.Model.CallRecord> to the entity class RegisterCall

class RegisterCall
+-------+------+--------------+
|Id     |Fname |    Place     |
+-------+------+--------------+
|   1   | Anna |   London     |
+-------+------+--------------+
|   2   | Sam  |   Paris      |
+-------+------+--------------+
|   5   | Tom  |   20.5       |
+-------+------+--------------+

//Thank you


What I have tried:

C#
private ReportContext db = new ReportContext();

        RegisterCall regcall= new RegisterCall();
        CallRecord callrecord= new CallRecord();

        // GET: Bill
        public ActionResult Index()
        {
           
            ViewBag.BUSelectedList = new SelectList(db.BUnitLists, "Id", "BUName");         

            return View(db.VariableVoices.ToList());
        }

// Post: Bill
        [HttpPost]
        public ActionResult BillSubmit(CallRecord  callrecords)
        {
 //how pass values from displayed values on page to class RegisterCall


         }

//Get BilView
 public ActionResult BilView()
        {


            return View(db.BillMaps.ToList());
        }






HTML
@using (Html.BeginForm("BillSubmit", "Bill", FormMethod.Post))
{
    <table class="table table-bordered">
        <thead>
            <tr>
                <th class="text-center">
                    Name
                </th>
                <th class="text-center">
                    Cost
                </th>
                <th class="text-center">Place</th>
            </tr>
        </thead>
     @foreach (var item in Model.GroupBy(un => un.fname, (key, items) => new { 
       fname= key, Cost = items.Sum(u => u.Cost) }))
        {
        <tbody>
            <tr>
             <td>
                  @Html.EditorFor(modelItem => item.fname, )
              </td>
            <td>
            @Html.EditorFor(modelItem => item.Cost)
            </td>
                <th>
 @Html.DropDownList("PlaceList", (IEnumerable<SelectListItem>)ViewBag.PlaceSelectList, "Select Business Unit", new { @class = "form-control" })
 @Html.ValidationMessage("PlaceList", "", new { @class = "text-danger" })

               </th>
                </tr>
            </tbody>
        }


    </table>

   
            <div>
                <input type="submit" value="SAVE" class="navbar-btn btn-primary"  />
            </div>

        

}
Posted
Updated 27-Mar-19 14:44pm
v2

In your [HTTPPost] SubmitBill method, the callRecords parameter contains the data submitted from the page.

After that, how you get it tinto the database depends on how you've implemented your data access layer (which you did not share with us).
 
Share this answer
 
Comments
JimB_ 27-Mar-19 20:44pm    
hi realJOSP could you please check again my question
This question is very similar to your previous one. You've to implement the same methods which were discussed here: How to use groupby data in view to display a result, in C#, MVC, LINQ[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900