在MVC中同时将数据写入两个表时遇到麻烦

时间:2019-06-12 10:03:27

标签: c# mysql model-view-controller

我正在处理MVC问题,用户将在其中将数据输入到表单中,该数据将存储在sql数据库中。数据库已经存在,有很多表,并且与我的asp.net连接。 我希望输入数据将保存到正确的表和正确的行中。这是我尝试的代码。数据微不足道。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using My_App.Models;

namespace My_App.Controllers
{
    public class RegistrationController : Controller
    {
        public ActionResult Registration()
        {
            Database1 db = new Database1();

            List<User> list = db.User.ToList();
            ViewBag.UserList = new SelectList(list, "id_car", "id_user", "name_user");

            return View();
        }

        [HttpPost]
        public ActionResult Registration(Car model)
        {
            try
            {
               Database1 db = new Database1();

            List<User> list = db.User.ToList();
            ViewBag.UserList = new SelectList(list, "id_car", "id_user", "name_user");

                Car cars = new Car();
cars.id_car = model.id_car;
cars.id_city = model.id_city;
cars.car_name = model.car_name;
db.Car.Add(cars);
db.SaveChanges;

      User users = new User();
users.id_car = model.id_car;
users.id_user = model.id_user;
users.name_user = model.name_user;
db.User.Add(users);
db.SaveChanges;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(model);
        }

    }
}

查看



        @model My_app.Models.Car
        @using (Html.BeginForm("Registration", "Registration", FormMethod.Post))
    {   
     @Html.AntiForgeryToken()
        
                 @Html.ValidationSummary(true)
            
                @Html.LabelFor(model=>model.id_car , new { @class = "control-label col-md-2" })
                

                    @Html.EditorFor((model=>model.id_car  )
                    @Html.ValidationMessageFor((model=>model.id_car  )
                
            


            
                @Html.LabelFor(model => model.car_name, new { @class = "control-label col-md-2" })
                
                   @Html.EditorFor(model => model.car_name)
                    @Html.ValidationMessageFor(model => model.car_name)
                
            

            
                @Html.LabelFor(model => model.name_user, new { @class = "control-label col-md-2" })
                
                    @Html.EditorFor(model => model.name_user)
                    @Html.ValidationMessageFor(model => model.name_user)
                
            

            
                
                    
                
            
          }

SELECT TOP (1000) [id_car]
    ,[id_city]
    ,[car_name]
  FROM [CarUsers].[dbo].[Car]

SELECT TOP (1000) [id_car]
    ,[id_user]
    ,[name_user]   
 FROM [CarUsers].[dbo].[User]

当我只为Car编写一个控制器时,它可以工作(数据已签名到我的SQL表中),但是当我仅为User编写它时,它不起作用(我将模型更改为User) 我也尝试过MultipleClass ..也行不通。我无法掌握问题的情况以及发生错误的地方。我希望将数据同时写入两个表中。 感谢您的宝贵时间和有用的建议。最好的祝福。

1 个答案:

答案 0 :(得分:0)

谢谢 这是一篇很棒的文章:)它解释了很多

我已将代码更改为

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using My_App.Models;

namespace My_App.Controllers
{
    public class RegistrationController : Controller
    {
[HttpPost]
        public ActionResult Registration()
        {
            Database1 db = new Database1();
              List<MyClass> MyClassList = new List<MyClass>();

            var details = (from c in db.Car
                           join u in db.User on c.id_car equals u.id_car
                           select new { c.id_car, c.id_city, c.car_name, u.id_user,u.name_user }).ToList();
            foreach (var item in details)
            {
                MyClass mC = new MyClass();
                mC.id_car = item.id_car;
                mC.id_city = item.id_city;
                mC.car_name = item.car_name;
                mC.id_user = item.id_user;
                mC.name_user = item.name_user;
                MyClassList.Add(mC); MyClassList.Add(mC);
              }

           return View(MyClassList);
        }

查看

 @model IEnumerable<My_app.Models.MyClass>
            @using (Html.BeginForm("Registration", "Registration", FormMethod.Post))
        {   
         @Html.AntiForgeryToken()

                     @Html.ValidationSummary(true)
        <div class="form-horizontal">


            @Html.ValidationSummary(true)

                 @foreach (var item in Model)
            {

                <div class="form-group">
                @Html.LabelFor(modelItem => item.id_car , new { @class = "control-label col-md-2" })
                <div class="col-md-10">

                    @Html.EditorFor(modelItem => item.id_car )
                    @Html.ValidationMessageFor(modelItem => item.id_car )
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(modelItem => item.car_name , new { @class = "control-label col-md-2" })
                <div class="col-md-10">

                    @Html.EditorFor(modelItem => item.car_name )
                    @Html.ValidationMessageFor(modelItem => item.car_name )
                </div>
            </div>


            <div class="form-group">
                @Html.LabelFor(modelItem => item.name_user, new { @class = "control-label col-md-2" })
                <div class="col-md-10">

                    @Html.EditorFor(modelItem => item.name_user)
                    @Html.ValidationMessageFor(modelItem => item.name_user)
                </div>
            </div>
    }

我现在需要找出如何将MYClass中的输入数据发布并保存到单个表(Car和User)中。我猜问题隐藏在Controller中并与之连接。添加吗?