Jon A
Jon A

Reputation: 137

Pass last created id from action to a partial view (asp.net mvc)

I have a little problem that I'm stuck on. I have 2 tables: tbl_NIR and tbl_I_O, tbl_I_O has a foreign key NirID, so I know which nir got that i_o.

I have an action Add and view, there I insert nir data into the database. Add action returns PartialView. In that PartialView I have new view for tbl_I_O.

I need to somehow get the last ID inserted which I should somehow get after I insert data in Add action. How can you pass ID from Add action to tbl_I_O partial view? I viewed this link and others but with no luck: "Asp.Net mvc - pass last created id from action to partial view".

Actions:

[HttpPost]
public ActionResult Add(BOL1.tbl_NIR nir)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.tbl_NIR.Add(nir);
                db.SaveChanges();
                //var ni = db.tbl_NIR.OrderByDescending(x => x.ID).FirstOrDefault().ID;
                 TempData["Msg"] = "Created Successfully!";
                return RedirectToAction("AddIO", nir);
                //return RedirectToAction("AddIO", new { id = ni });
                //return RedirectToAction("AddIO", "AddIO", new { id = ni });
            }
            return View(nir);

and the ohter:

[HttpGet]
    public ActionResult AddIO()
    {
        //var ni = db.tbl_I_O.OrderByDescending(x => x.NirID).FirstOrDefault().NirID;
        //return View(ni);
        return View();
    }



[HttpPost]
    public ActionResult AddIO(BOL1.tbl_I_O oi)
    //public ActionResult AddIO(BOL1.tbl_I_O oi, int ID)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.tbl_I_O.Add(oi);
                db.SaveChanges();
                TempData["Msg"] = "Created Successfully!";
                return RedirectToAction("Index");
            }
          return View(oi);
        }

and this is the part of code from my view where I need to display the last inserted ID from tbl_NIR (action Add) to tbl_I_O (action AddIO partial view):

 <div class="form-group">
                @Html.LabelFor(model => model.NirID, "Nir ID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*@Html.DropDownList("NirID", null, htmlAttributes: new { @class = "form-control" })*@
                    @Html.EditorFor(model => model.NirID, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.NirID, "", new { @class = "text-danger" })
                </div>
</div>

So any help on it is really appriciated. Sorry for any bad spelling!

Here is my update on this (the view and the partial view):

@model BOL1.tbl_NIR

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    @*@{int i = 0;}*@

    <div class="form-horizontal">
        <h2>Add New</h2>
        <br />
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
                @Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
                    @*<span>@(++i)</span>*@
                    @Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
                </div>
</div>

        <div class="form-group">
                @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
                </div>
</div>        

        <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
}

@*I 'ved commented this section because it displays the partial view in the same view as VIEW (Add controller) and I don't want that, I want them to be on separate views.*@
@*<div class="form-group">
    @Html.Partial("AddIO", new BOL1.tbl_I_O())
</div>*@

<div>
        @Html.ActionLink(" Back to List", "Index", null, new { @class = "glyphicon glyphicon-chevron-left" })
    </div>

and the partial view:

@model BOL1.tbl_I_O
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <br />
            <h5>Add new Entry or Exit</h5>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
                @Html.LabelFor(model => model.NirID, "Nir ID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*@Html.DropDownList("NirID", null, htmlAttributes: new { @class = "form-control" })*@
                    @Html.EditorFor(model => model.NirID, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.NirID, "", new { @class = "text-danger" })
                </div>
</div>

        <div class="form-group">
                @Html.LabelFor(model => model.TipID, "Type ID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*@Html.DropDownListFor(model => model.TipID, (IEnumerable<SelectListItem>)ViewBag.TID, new { @class = "form-control" })*@
                    @Html.EditorFor(model => model.TipID, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.TipID, "", new { @class = "text-danger" })
                </div>
</div>

        <div class="form-group">
                @Html.LabelFor(model => model.SupplierID, "Supplier ID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*@Html.DropDownListFor(model => model.SupplierID, (IEnumerable<SelectListItem>)ViewBag.SID, new { @class = "form-control" })*@
                    @Html.EditorFor(model => model.SupplierID, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SupplierID, "", new { @class = "text-danger" })
                </div>
</div>

        <div class="form-group">
                @Html.LabelFor(model => model.ProductID, "Product ID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*@Html.DropDownListFor(model => model.ProductID, (IEnumerable<SelectListItem>)ViewBag.PID, new { @class = "form-control" })*@
                    @Html.EditorFor(model => model.ProductID, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ProductID, "", new { @class = "text-danger" })
                </div>
</div>

        <div class="form-group">
                @Html.LabelFor(model => model.EntryDate, "Entry Date", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.EntryDate, new { htmlAttributes = new { @class = "form-control" } })
                    @*<input class="datefield" data-val="true" id="EntryDate" name="EntryDate" type="date" value=" " />*@
                    @Html.ValidationMessageFor(model => model.EntryDate, "", new { @class = "text-danger" })
</div>

        <div class="form-group">
                @Html.LabelFor(model => model.ExitDate, "Exit Date", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ExitDate, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ExitDate, "", new { @class = "text-danger" })
                </div>
</div>

        <div class="form-group">
            <font color="#D3D1D1">
                @Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
            </font>
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
                </div>
</div>

        <div class="form-group">
                @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
                </div>
</div>

        @*<div class="form-group">
                @Html.LabelFor(model => model.Total, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Total, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Total, "", new { @class = "text-danger" })
                </div>
</div>*@

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink(" Back to List", "Index", null, new { @class = "glyphicon glyphicon-chevron-left" })
</div>

Basically what I need is, when I create the new row for tbl_NIR (e.g.: ID = 23 (autoincrement), blah blah), the value from the ID ("23") to be displayed in the partial view on the:

@Html.EditorFor(model => model.NirID, new { htmlAttributes = new { @class = "form-control" } })

field. I think that maybe the "EdidorFor is the problem"!?

Upvotes: 3

Views: 654

Answers (1)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48417

You cannot redirect to PartialView.

[HttpPost]
public ActionResult Add(BOL1.tbl_NIR nir)
{
        if (ModelState.IsValid)
        {
            db.tbl_NIR.Add(nir);
            db.SaveChanges();
            Session["id"]=nir.ID;
            return RedirectToAction("AddIO");
        }
    return View(nir);
}

C# -pass a new BOL1.tbl_I_O object.

[HttpGet]
public ActionResult AddIO()
{
    return View(new BOL1.tbl_I_O());
}

HTML

Change

@Html.EditorFor(model => model.NirID, new { htmlAttributes = new { @class = "form-control" } })

To

<input type="text" id="NirID" name="NirID" class="form-control" value="@Session["id"]"/>

Upvotes: 2

Related Questions