Asp.net MVC +复选框插入数据

本文关键字:插入 数据 复选框 net MVC Asp | 更新日期: 2023-09-27 18:08:58

我有一个问题。我有3个模型(Person, RegisteredAddress和addresscorresponence)。RegisterAddress和addresscorresponence是完全相同的表(具有相同的列名称)。Person模型为:

    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName{ get; set; }
    public DateTime BirthDate{ get; set; }
    public bool TheSameAddress{ get; set; }

Addresses的模型

    public int Id { get; set; }
    public string Street{ get; set; }
    public int Number{ get; set; }
    public string Code{ get; set; }
    public string State{ get; set; }
    public string City{ get; set; }
    public int Person_Id { get; set; }

然后我有3个视图。每个视图都是一个响应每个模型的表单。在第一个视图中,我们可以标记"TheSameAddress"属性的复选框值。我想做的是:当用户标记这个复选框,然后我想打开只有一个视图与RegisteredAddress,但我想保存相同的数据到AddressCorrespondence表。这意味着当复选框被标记时,应该只显示第二个表单(在第一个之后),但第三个视图/表单不能显示,但在数据库中应该保存日期。有没有人知道我该怎么做/解决我的问题?

Asp.net MVC +复选框插入数据

选项1

如果我理解的话,你可以这样做:

视图中的

:人员注册视图表单设计如您所愿,您可以隐藏或显示第三个表单,如果复选框启用或不启用

@model WebApplication2.Models.Person
@{ 
    ViewBag.Title = "Register Person";
}
@using (Html.BeginForm())
{
    <div class="col-lg-12"><br /></div>
        <label>FirstName</label>
        @Html.TextBoxFor(m=>m.FirstName)
    <div class="col-lg-12"><br /></div>
        <label>LastName</label>
        @Html.TextBoxFor(m => m.LastName )
    <div class="col-lg-12"><br /></div>
       <label>The Same Address?</label>
        @Html.CheckBoxFor(m=>m.TheSameAddress)
    <div class="col-lg-12"><br /></div>
    <label>City</label>
    <input type="text" name="City"id="City" />
    <div class="col-lg-12"><br /></div>
    <label>Code</label>
    <input type="text" name="Code" id="Code" />
    <div class="col-lg-12"><br /></div>
    <label>Number</label>
    <input type="number" name="Number" id="Number" />
    <div class="col-lg-12"><br /></div>
    <input type="submit" value="Save" />
}

控制器

比较如果checkbox为真,则将数据添加到RegisteredAddressAddressCorrespondence,否则仅将数据添加到RegisteredAddress

        public ActionResult CreatePeople()
        {
            return View();
        }
        [HttpPost]
        public ActionResult CreatePeople(Person person,string city,string code , int number)
        {
            if (person.TheSameAddress == true )
            {
                var newperson = new Person
                {
                    FirstName = person.FirstName,
                    LastName = person.LastName,
                     BirthDate= DateTime.Now,
                    TheSameAddress=person.TheSameAddress                                                
                };
                db.person.Add(newperson);
                db.SaveChanges();
                var newRegisterAdress = new RegisteredAddress
                {
                    City = city,
                    Code = code,
                    Number = number,
                    Person = newperson
                };
                db.RegisteredAddress.Add(newRegisterAdress);
                db.SaveChanges();
                var newAdressCorrsp = new AddressCorrespondence
                {
                    City = city,
                    Code = code,
                    Number = number,
                    Person = newperson
                };
                db.AddressCorrespondence.Add(newAdressCorrsp);
                db.SaveChanges();
            }
            else
            {
                var newperson = new Person
                {
                    FirstName = person.FirstName,
                    LastName = person.LastName,
                    BirthDate = DateTime.Now,
                    TheSameAddress=person.TheSameAddress
                };
                db.person.Add(newperson);
                var newRegisterAdress = new RegisteredAddress
                {
                    City = city,
                    Code = code,
                    Number = number,
                    Person = newperson
                };
                db.RegisteredAddress.Add(newRegisterAdress);
                db.SaveChanges();
            }
            return View();
        }

它是一个想法,你可以怎么做,可以适应三个视图,如果你想

如果你想在其他视图中分配数据

选项2

首先从索引视图注册人员并分配地址,index =People

:IndexView

@model IEnumerable<WebApplication2.Models.Person>
@{
    ViewBag.Title = "People";
}
<table class="table">
    <tr>
        <th>
            <label>Name</label>
        </th>
        <th>
            <label>LastName</label>
        </th>
        <th>
            <label>BirthDate</label>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName )
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName )
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.BirthDate)
            </td>
            <td>
                @Html.ActionLink("Add Adress", "CreateAdress", new { Id_Person = item.ID }, htmlAttributes: new { @class = "btn btn-primary btn-xs" })
            </td>
        </tr>
    }
</table>

控制器

        public ActionResult CreateAdress(int Id_Person )
        {
            ViewBag.PersonId = Id_Person;
            return View();
        }
        [HttpPost]
        public ActionResult CreateAdress(RegisteredAddress ra ,int Id_Person)
        {
            var personf = db.person.Find(Id_Person);
            if (personf.TheSameAddress == true)
            {
                var newRegisterAdress = new RegisteredAddress
                {
                    City = ra.City ,
                    Code = ra.Code,
                    Number = ra.Number,
                    Person = personf
                };
                db.RegisteredAddress.Add(newRegisterAdress);
                db.SaveChanges();
                var newAdressCorrsp = new AddressCorrespondence
                {
                    City = ra.City,
                    Code = ra.Code,
                    Number = ra.Number,
                    Person = personf
                };
                db.AddressCorrespondence.Add(newAdressCorrsp);
                db.SaveChanges();
            }
            else
            {
                var newRegisterAdress = new RegisteredAddress
                {
                    City = ra.City,
                    Code = ra.Code,
                    Number = ra.Number,
                    Person = personf
                };
                db.RegisteredAddress.Add(newRegisterAdress);
                db.SaveChanges();
            }
            return RedirectToAction("People");
        }

我在这里留下了一个项目链接