-1

コントローラーコード:

private UBADBEntities db = new UBADBEntities();

    public ActionResult Index()
    {
        ViewBag.Pos1 = new SelectList(db.Positions, "posID", "posName");
        ViewBag.Pos2 = new SelectList(db.Positions, "posID", "posName");
        var applications = db.Applications.Include("Position").Include("Position1");
        return View(applications);
    }

    [HttpPost]
    public ActionResult Index(int id, int listPos1, int listPos2)
    {
        var applicationQuery =
            from app in db.Applications
            where app.CandidateID == id
            select app;

        ViewBag.Pos1 = new SelectList(db.Positions, "posID", "posName", listPos1);
        ViewBag.Pos2 = new SelectList(db.Positions, "posID", "posName", listPos2);

        foreach (var application in applicationQuery)
        {
            if (application.CandidateID == id)
            {
                application.Pos1 = listPos1;
                application.Pos2 = listPos2;
            }
        }

        db.SaveChanges();
        return View(db.Applications.ToList());
    }

コードを表示:

<div id="firstPos-@item.CandidateID.ToString()" class="show">@Html.DisplayFor(modelItem => item.Position.posName)</div>
<div id="listPos1-@item.CandidateID.ToString()" class="invisible">@Html.DropDownList("Pos1", String.Empty)</div>
@Html.DisplayFor(modelItem => item.Position1.posName)
<div id="listPos2-@item.CandidateID.ToString()" class="invisible">@Html.DropDownList("Pos2", String.Empty)</div>

JS スクリプト:

function ShowEdit(par) {
        $("#listPos1-" + par).attr("class", "show");
        $("#secondPos-" + par).attr("class", "invisible");
        $("#listPos2-" + par).attr("class", "show");
        $("#btnEdit-" + par).attr("class", "btn-toolbar text-center btn-group btn hide");
        $("#btnSave-" + par).attr("class", "btn-toolbar text-center btn-group btn show");
    }
    function SaveEdit(par) {
        $("#listPos1-" + par).attr("class", "invisible");
        $("#secondPos-" + par).attr("class", "show");
        $("#listPos2-" + par).attr("class", "invisible");
        $("#btnEdit-" + par).attr("class", "btn-toolbar text-center btn-group btn show");
        $("#btnSave-" + par).attr("class", "btn-toolbar text-center btn-group btn hide");

        var _listPos1 = $("#listPos1-" + par).val();
        var _listPos2 = $("#listPos2-" + par).val();
        var url = '@Url.Action("Index","Application")';
        $.post(url, { id: par listPos1: _listPos1, listPos2: _listPos2 }, function (data) {
            $("#divResult").html(data);
        });
    }
4

2 に答える 2