如何从表单,MVC,C#,linq,SQL提交值 [英] How to submit value from form, MVC, C#, linq, SQL

查看:46
本文介绍了如何从表单,MVC,C#,linq,SQL提交值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

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());
        }



















@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>

        

}

推荐答案

[HTTPPost] SubmitBill 方法, callRecords 参数包含从页面提交的数据。



之后那你如何得到数据库取决于你如何实现你的数据访问层(你没有与我们分享)。
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).


这个问题与你以前的问题非常相似。您必须实现此处讨论的相同方法:如何在视图中使用groupby数据以C#,MVC,LINQ 显示结果[ ^ ]
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[^]


这篇关于如何从表单,MVC,C#,linq,SQL提交值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆