MVC额外的帖子,用于更新视图

时间:2018-06-19 11:30:56

标签: post model-view-controller view get controllers

我有一个具有Get and Post操作的控制器。后期操作在框架上节省了动物。在表单中,除了用于更改框架中的已知标识的列表之外,我还有一个列表可用于在框架中添加其他标识。

我在视图中的第一个循环的末尾有一个按钮,我想用它在视图中添加新行并填充 公开列表NieuweIdentificatieinfos {get;组; } 还需要对视图中未提及的另一个列表执行此操作。

问题是:我该怎么做? 我是否需要一个采用表格并在表格中填写具体清单并再次返回视图的新控制器?

使用按钮和相同的动作名称可以吗?我尝试创建一个新的控制器,但是如果它的动作名称和参数相同,则将无法正常工作。 我尝试使用动作链接,但是我不知道如何在视图中填写表格。

获取->使用零件的ID创建视图

[HttpGet]
    public ActionResult DierBewerken(int id)
    {

        DierInfoService dierinfoservice = new DierInfoService();
        var nieuweform = new DierInfoGrootForm();


        List<Identificatie> identificatielijst = dierinfoservice.GetIdentificatieViaId(id);
        List<IdentificatieInfo> identificatieinfoLijst = new List<IdentificatieInfo>();

        foreach (Identificatie identificiatie in identificatielijst)
        {

            if (identificiatie.TypeIdentificatie == "CHIP")
            { typeidentificatie = IdentificatieInfo.TypeIdentificatieEnum.CHIP; }
            else
            { typeidentificatie = IdentificatieInfo.TypeIdentificatieEnum.TATOEAGE; }


            if (identificiatie.TatoeagePlaats == "NEK")
            {
                tatoeageplaats = IdentificatieInfo.IdentificiatiePlaats.NEK;
            }
            else if (identificiatie.TatoeagePlaats == "OOR")
            {
                tatoeageplaats = IdentificatieInfo.IdentificiatiePlaats.OOR;
            }
            else if (identificiatie.TatoeagePlaats == "BIL")
            {
                tatoeageplaats = IdentificatieInfo.IdentificiatiePlaats.BIL;
            }
            else
            {
                tatoeageplaats = null;
            }

            if (identificiatie.TatoeageAanduiding == "LINKS")
            {
                tatoeageaanduiding = IdentificatieInfo.IdentificiatieAanduiding.LINKS;
            }
            else if (identificiatie.TatoeageAanduiding == "RECHTS")
            {
                tatoeageaanduiding = IdentificatieInfo.IdentificiatieAanduiding.RECHTS;
            }
            else
            {
                tatoeageaanduiding = null;
            }

            if (identificiatie.ChipPlaats == "NEK")
            {
                chipplaats = IdentificatieInfo.IdentificiatiePlaats.NEK;
            }
            else if (identificiatie.ChipPlaats == "OOR")
            {
                chipplaats = IdentificatieInfo.IdentificiatiePlaats.OOR;
            }
            else if (identificiatie.ChipPlaats == "BIL")
            {
                chipplaats = IdentificatieInfo.IdentificiatiePlaats.BIL;
            }
            else
            {
                chipplaats = null;
            }

            if (identificiatie.ChipAanduiding == "LINKS")
            {
                chipaanduiding = IdentificatieInfo.IdentificiatieAanduiding.LINKS;
            }
            else if (identificiatie.ChipAanduiding == "RECHTS")
            {
                chipaanduiding = IdentificatieInfo.IdentificiatieAanduiding.RECHTS;
            }
            else
            {
                chipaanduiding = null;
            }

            identificatieinfoLijst.Add(
                new IdentificatieInfo(identificiatie.IdentificatieID, typeidentificatie, identificiatie.TatoeageNummer, tatoeageplaats,
                tatoeageaanduiding, identificiatie.ChipNummer, chipplaats, chipaanduiding));
        }

        List<FysischeKenmerkenInfo> fysischeKenmerkeninfoKortLijst = new List<FysischeKenmerkenInfo>();
        fysischeKenmerkeninfoKortLijst = dierinfoservice.GetFysischeKenmerkenKortViaId(id);


        string NUllwaarde;
        int NUllID;
        Dier dier = db.Dier.Find(id);
        //var fysischeKenmerken = db.FysischeKenmerken.Where(m => m.Dier_DierID == id).ToList();
        DierInfo dierinfo = new DierInfo();
        if (dier.Ras1 == null)
        {
            NUllwaarde = "";
            NUllID = 0;
        }
        else
        {
            NUllwaarde = dier.Ras1.RasNaam;
            NUllID = (int)dier.Ras_RasIDKruising;
        }
        dierinfo.DierID = id;
        dierinfo.Diernaam = dier.Diernaam;
        dierinfo.Geboortdatum = dier.Geboortdatum;
        dierinfo.Geslacht = dier.Geslacht;
        dierinfo.Kruising = dier.Kruising;
        dierinfo.VermoedelijkeGeboortejaar = dier.VermoedelijkeGeboortejaar;
        dierinfo.RasID2 = NUllID;
        dierinfo.RasID = dier.Ras_RasID;

        //ViewBag.HerkenningsObject_HerkenningsObjectID = new SelectList(db.HerkenningsObject, "HerkenningsObjectID", "HerkenningsObjectKenmerk" , ); ;
        ViewBag.Ras_RasID = new SelectList(db.Ras, "RasID", "RasNaam", dier.Ras_RasID);
        ViewBag.Ras_RasIDKruising = new SelectList(db.Ras, "RasID", "RasNaam", dier.Ras_RasIDKruising);

        //ViewBag.Ras_RasID = new SelectList(db.HerkenningsObject, "HerkenningsObjectID", "HerkenningsObjectKenmerk", fysischeKenmerken. );
        //ViewBag.Ras_RasIDKruising = new SelectList(db.Ras, "RasID", "RasNaam", dier.Ras_RasIDKruising);

        nieuweform.Identificatieinfos = identificatieinfoLijst;
        nieuweform.FysischeKenmerkenInfos = fysischeKenmerkeninfoKortLijst;
        nieuweform.Dierinfo = dierinfo;
        nieuweform.NieuweFysischeKenmerkenInfos = new List<FysischeKenmerkenInfo>();
        nieuweform.NieuweIdentificatieinfos = new List<IdentificatieInfo>();

        return View(nieuweform);

    }

发布->将表单保存到框架

[HttpPost]
    //[ValidateAntiForgeryToken]
    public ActionResult DierBewerken(DierInfoGrootForm form)
                //public ActionResult DierBewerken([Bind(Include = "DierID,Ras_RasID,Kruising,Ras_RasIDKruising,Diernaam,Geboortdatum,VermoedelijkeGeboortejaar,Geslacht")] Dier dier)
    {

        if (ModelState.IsValid)
        {


            foreach (var item in form.Identificatieinfos)
            {
                Identificatie identificatie = new Identificatie
                {
                    Dier_DierID = form.Dierinfo.DierID,
                    IdentificatieDatum = DateTime.Now,
                    IdentificatieID = item.IdentificatieID,
                    TypeIdentificatie = item.TypeIdentificatieEnumeration.ToString(),
                    TatoeageNummer = item.TatoeageNummer,
                    TatoeagePlaats = item.TatoeagePlaats.ToString() ?? null,
                    TatoeageAanduiding = item.TatoeageAanduiding.ToString() ?? null,
                    ChipNummer = item.ChipNummer,
                    ChipPlaats = item.ChipPlaats.ToString() ?? null,
                    ChipAanduiding = item.ChipAanduiding.ToString() ?? null
                };
                db.Entry(identificatie).State = EntityState.Modified;

            }

            Dier dier = new Dier
            {
                DierID = form.Dierinfo.DierID,
                Diernaam = form.Dierinfo.Diernaam,
                Geslacht = form.Dierinfo.Geslacht,
                Kruising = form.Dierinfo.Kruising,
                Ras_RasID = form.Dierinfo.RasID,
                Ras_RasIDKruising = form.Dierinfo.RasID2,
                Geboortdatum = form.Dierinfo.Geboortdatum,
                VermoedelijkeGeboortejaar = form.Dierinfo.VermoedelijkeGeboortejaar
            };


            db.Entry(dier).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("OverzichtDieren");
        }
        //ViewBag.Ras_RasID = new SelectList(db.Ras, "RasID", "RasNaam", dier.Ras_RasID);
        //ViewBag.Ras_RasIDKruising = new SelectList(db.Ras, "RasID", "RasNaam", dier.Ras_RasIDKruising);
        return View(form);
        //return View(dier);
    }

表单类

namespace Dierenasiel_MVC_Eindwerk.Models
{
 public class DierInfoGrootForm
{
    public int DierId { get; set; }
    public DierInfo Dierinfo { get; set; }
    public List<IdentificatieInfo> Identificatieinfos { get; set; }
    public List<FysischeKenmerkenInfo> FysischeKenmerkenInfos { get; set; }
    public List<IdentificatieInfo> NieuweIdentificatieinfos { get; set; }
    public List<FysischeKenmerkenInfo> NieuweFysischeKenmerkenInfos { get; set; }

    public List<Herkenningsobjecten> Herkenningsobjectens { get; set; }


}
}

我使用列表来更改标识和添加标识的视图的一部分。

@for (int index = 0; index < Model.Identificatieinfos.Count(); index++)


                {

                    @Html.HiddenFor(model => model.Identificatieinfos[index].IdentificatieID)
                    <tr>
                        <th scope="row">@(index + 1)</th>
                        @*<td>@Html.EditorFor(model => model.Identificatieinfos[i], new { htmlAttributes = new { @class = "form-control" } })</td>*@
                        @*<td>@Html.DropDownListFor(m => m.Identificatieinfos[i].TypeEn , new SelectList( Enum.GetValues()  )),"Selecteer identificatie")</td>*@
                        <td>
                            @Html.EnumDropDownListFor(model => model.Identificatieinfos[index].TypeIdentificatieEnumeration, "Selecteer identificatie", new { @class = "fields-select form-control" ,
                           @onchange = "onSelectChange(this , index);"
                       })
                        </td>
                        @if (@Model.Identificatieinfos[index].TypeIdentificatieEnumeration.ToString() == "CHIP")
                        {
                            <td>@Html.EditorFor(model => model.Identificatieinfos[index].ChipNummer, new { htmlAttributes = new { @class = "form-control" } })</td>
                            <td>@Html.EnumDropDownListFor(model => model.Identificatieinfos[index].ChipPlaats, "Selecteer de plaats", new { @class = "fields-select form-control" })</td>
                            <td>@Html.EnumDropDownListFor(model => model.Identificatieinfos[index].ChipAanduiding, "Selecteer de positie", new { @class = "fields-select form-control" })</td>
                            <td class="d-none">@Html.EditorFor(model => model.Identificatieinfos[index].TatoeageNummer, new { htmlAttributes = new { @class = "form-control" } })</td>
                            <td class="d-none">@Html.EnumDropDownListFor(model => model.Identificatieinfos[index].TatoeagePlaats, "Selecteer de plaats", new { @class = "fields-select form-control" })</td>
                            <td class="d-none">@Html.EnumDropDownListFor(model => model.Identificatieinfos[index].TatoeageAanduiding, "Selecteer de positie", new { @class = "fields-select form-control" })</td>
                        }
                        else if (@Model.Identificatieinfos[index].TypeIdentificatieEnumeration.ToString() == "TATOEAGE")
                        {
                            <td class="d-none">@Html.EditorFor(model => model.Identificatieinfos[index].ChipNummer, new { htmlAttributes = new { @class = "form-control" } })</td>
                            <td class="d-none">@Html.EnumDropDownListFor(model => model.Identificatieinfos[index].ChipPlaats, "Selecteer de plaats", new { @class = "fields-select form-control" })</td>
                            <td class="d-none">@Html.EnumDropDownListFor(model => model.Identificatieinfos[index].ChipAanduiding, "Selecteer de positie", new { @class = "fields-select form-control" })</td>
                            <td>@Html.EditorFor(model => model.Identificatieinfos[index].TatoeageNummer, new { htmlAttributes = new { @class = "form-control" } })</td>
                            <td>@Html.EnumDropDownListFor(model => model.Identificatieinfos[index].TatoeagePlaats, "Selecteer de plaats", new { @class = "fields-select form-control" })</td>
                            <td>@Html.EnumDropDownListFor(model => model.Identificatieinfos[index].TatoeageAanduiding, "Selecteer de positie", new { @class = "fields-select form-control" })</td>
                        }

                    </tr>
                    //index++;
                }

                <tr class="col" id="IdentificatieToevoegen">
                    <th scope="row">#</th>
                    <td colspan="4">



                        <div class="btn btn-friendly form-control text-left" name="1" value="1">@Html.ActionLink("Identificatie toevoegen", "DierBewerken", new { Bijvoegen = "identificatie" })</div>




                    </td>
                </tr>
            </tbody>
        </table>
        @*@Model.NieuweIdentificatieinfos.Add(new Dierenasiel_MVC_Eindwerk.Models.HerdefinerenClasses.IdentificatieInfo("TATOEAGE", "15qsdq14", "NEK", "LINKS", null, null, null))*@
        @*Dierenasiel_MVC_Eindwerk.Models.HerdefinerenClasses.IdentificatieInfo.TypeIdentificatieEnum.TATOEAGE*@
        @*//new Dierenasiel_MVC_Eindwerk.Models.HerdefinerenClasses.IdentificatieInfo.TypeIdentificatieEnum.*@
    </div> @*div table identificatie*@

    @if (Model.NieuweIdentificatieinfos.Count() > 0)
    {
        <div class="form-group col">

            <table class="table table-striped">
                <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">Type identificatie</th>
                        <th scope="col">Nummer</th>
                        <th scope="col">Plaats</th>
                        <th scope="col">Zijde</th>
                    </tr>
                </thead>
                <tbody>

                    @for (int index = 0; index < Model.NieuweIdentificatieinfos.Count(); index++)

                    //foreach (var item in @ViewBag.IdentificatieinfoLijst)
                    {

                        @*@Html.HiddenFor(model => model.NieuweIdentificatieinfos[index].IdentificatieID)*@
                        <tr>
                            <th scope="row">@(index + 1)</th>
                            @*<td>@Html.EditorFor(model => model.Identificatieinfos[i], new { htmlAttributes = new { @class = "form-control" } })</td>*@
                            @*<td>@Html.DropDownListFor(m => m.Identificatieinfos[i].TypeEn , new SelectList( Enum.GetValues()  )),"Selecteer identificatie")</td>*@
                            <td>
                                @Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].TypeIdentificatieEnumeration, "Selecteer identificatie", new { @class = "fields-select form-control" ,
                               @onchange = "onSelectChange(this , index);"
                           })
                            </td>
                            @if (@Model.NieuweIdentificatieinfos[index].TypeIdentificatieEnumeration.ToString() == "CHIP")
                            {
                                <td>@Html.EditorFor(model => model.NieuweIdentificatieinfos[index].ChipNummer, new { htmlAttributes = new { @class = "form-control" } })</td>
                                <td>@Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].ChipPlaats, "Selecteer de plaats", new { @class = "fields-select form-control" })</td>
                                <td>@Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].ChipAanduiding, "Selecteer de positie", new { @class = "fields-select form-control" })</td>
                                <td class="d-none">@Html.EditorFor(model => model.NieuweIdentificatieinfos[index].TatoeageNummer, new { htmlAttributes = new { @class = "form-control" } })</td>
                                <td class="d-none">@Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].TatoeagePlaats, "Selecteer de plaats", new { @class = "fields-select form-control" })</td>
                                <td class="d-none">@Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].TatoeageAanduiding, "Selecteer de positie", new { @class = "fields-select form-control" })</td>
                            }
                            else if (@Model.NieuweIdentificatieinfos[index].TypeIdentificatieEnumeration.ToString() == "TATOEAGE")
                            {
                                <td class="d-none">@Html.EditorFor(model => model.NieuweIdentificatieinfos[index].ChipNummer, new { htmlAttributes = new { @class = "form-control" } })</td>
                                <td class="d-none">@Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].ChipPlaats, "Selecteer de plaats", new { @class = "fields-select form-control" })</td>
                                <td class="d-none">@Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].ChipAanduiding, "Selecteer de positie", new { @class = "fields-select form-control" })</td>
                                <td>@Html.EditorFor(model => model.NieuweIdentificatieinfos[index].TatoeageNummer, new { htmlAttributes = new { @class = "form-control" } })</td>
                                <td>@Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].TatoeagePlaats, "Selecteer de plaats", new { @class = "fields-select form-control" })</td>
                                <td>@Html.EnumDropDownListFor(model => model.NieuweIdentificatieinfos[index].TatoeageAanduiding, "Selecteer de positie", new { @class = "fields-select form-control" })</td>
                            }

                        </tr>
                        //index++;
                    }
                </tbody>
            </table>       

        </div> @*div table identificatie*@


    }

0 个答案:

没有答案