R
R
reboot7772019-06-18 07:26:37
ASP.NET
reboot777, 2019-06-18 07:26:37

How to link the row id of the main table to a table with multiple columns?

Good afternoon.
I am writing a project using asp.net mvc.
Added database, model. Controllers and views at the same time.
In the health check, there is no option in the view to add multiple records per person.
That is, we have two tables.
1. Main table. Columns: id_chelovek, full name, addresses.
2. Appeals. Columns: id_chelovek, Request date, Request reason, Completion mark.
3. Task: so that appeals cling to one record per person.
4. I don't understand how to implement it in C#. In controller and model.
Attached is the controller and model code.

namespace tson10.Controllers
{
    public class peopleController : Controller
    {
        private tsonEntities4 db = new tsonEntities4();

        // GET: people
        public async Task<ActionResult> Index()
        {
            var people = db.people.Include(p => p.category).Include(p => p.child).Include(p => p.city).Include(p => p.disability).Include(p => p.firing).Include(p => p.status).Include(p => p.street).Include(p => p.treatment);
            return View(await people.ToListAsync());
        }

        // GET: people/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            person person = await db.people.FindAsync(id);
            if (person == null)
            {
                return HttpNotFound();
            }
            return View(person);
        }

        // GET: people/Create
        public ActionResult Create()
        {
            ViewBag.id_category = new SelectList(db.categories, "id_category", "Категория");
            ViewBag.id_chelovek = new SelectList(db.children, "id_chelovek", "Фамилия");
            ViewBag.id_city = new SelectList(db.cities, "id_city", "Населенный_пункт");
            ViewBag.id_disability = new SelectList(db.disabilities, "id_disability", "Инвалидность");
            ViewBag.id_firing = new SelectList(db.firings, "id_firing", "Отопление");
            ViewBag.id_status = new SelectList(db.status, "id_status", "Статус");
            ViewBag.id_street = new SelectList(db.streets, "id_street", "Улица");
            ViewBag.id_chelovek = new SelectList(db.treatments, "id_chelovek", "Дата_обращения", "Причина_обращения", "Отметка_о_выполнении");
            return View();
        }

        // POST: people/Create
        // Чтобы защититься от атак чрезмерной передачи данных, включите определенные свойства, для которых следует установить привязку. Дополнительные 
        // сведения см. в статье https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "id_chelovek,Фамилия,Имя,Отчество,Дата_рождения,Телефон,id_category,id_disability,id_city,id_street,Дом,Квартира,id_firing,id_status")] person person)
        {
            if (ModelState.IsValid)
            {
                db.people.Add(person);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.id_category = new SelectList(db.categories, "id_category", "Категория", person.id_category);
            ViewBag.id_chelovek = new SelectList(db.children, "id_chelovek", "Фамилия", person.id_chelovek);
            ViewBag.id_city = new SelectList(db.cities, "id_city", "Населенный_пункт", person.id_city);
            ViewBag.id_disability = new SelectList(db.disabilities, "id_disability", "Инвалидность", person.id_disability);
            ViewBag.id_firing = new SelectList(db.firings, "id_firing", "Отопление", person.id_firing);
            ViewBag.id_status = new SelectList(db.status, "id_status", "Статус", person.id_status);
            ViewBag.id_street = new SelectList(db.streets, "id_street", "Улица", person.id_street);
            ViewBag.id_chelovek = new SelectList(db.treatments, "id_chelovek", "Причина_обращения", person.id_chelovek);
            return View(person);


Main table model code:

namespace tson10.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class person
    {
        public int id_chelovek { get; set; }
        public string Фамилия { get; set; }
        public string Имя { get; set; }
        public string Отчество { get; set; }
        public Nullable<System.DateTime> Дата_рождения { get; set; }
        public string Телефон { get; set; }
        public Nullable<int> id_category { get; set; }
        public Nullable<int> id_disability { get; set; }
        public Nullable<int> id_city { get; set; }
        public Nullable<int> id_street { get; set; }
        public string Дом { get; set; }
        public string Квартира { get; set; }
        public Nullable<int> id_firing { get; set; }
        public Nullable<int> id_status { get; set; }
    
        public virtual category category { get; set; }
        public virtual child child { get; set; }
        public virtual city city { get; set; }
        public virtual disability disability { get; set; }
        public virtual firing firing { get; set; }
        public virtual status status { get; set; }
        public virtual street street { get; set; }
        public virtual treatment treatment { get; set; }
    }
}

Call Model CODE:

namespace tson10.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class treatment
    {
        public int id_chelovek { get; set; }
        public Nullable<System.DateTime> Дата_обращения { get; set; }
        public string Причина_обращения { get; set; }
        public string Отметка_о_выполнении { get; set; }
    
        public virtual person person { get; set; }
    }
}

I will clarify. The goal is to "bind" the id_chelovek of the main table to the columns of the Cases table. What happened like this:
Ivanov Ivan Ivanovich
00.00.00.
id_chelovek - Appeal 1
id_chelovek - Appeal 2
id_chelovek - Appeal 3
...
Stupid question, I admit..but I want to understand it. How to implement in C# in controller?
Or how to implement it at all?
Thank you for your attention.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question