将最近创建的id从Action传递到部分视图(ASP.NET MVC)

3

我有一个遇到困难的小问题。我有两个表:tbl_NIR和tbl_I_O,tbl_I_O有一个外键NirID,所以我知道是哪个nir得到了这个i_o。

我有一个添加(Add)和查看(View)操作,在那里我将nir数据插入数据库。添加操作返回PartialView。在这个PartialView中,我有一个新的tbl_I_O视图。

我需要以某种方式获取最后插入的ID,在我执行添加操作后我应该以某种方式获取它。如何将ID从添加操作传递到tbl_I_O部分视图?我查看过这个链接和其他链接,但没有成功:"Asp.Net mvc - pass last created id from action to partial view"。

动作:

[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);

另外一个:

[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);
        }

这是我的代码视图中需要展示从tbl_NIR(操作Add)插入的最后一个ID到tbl_I_O(操作AddIO部分视图)的部分:

 <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>

非常感谢您的帮助。对于可能存在的错别字,我表示抱歉!

以下是关于此问题(视图和部分视图)的最新信息:

@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>

以及部分视图:

@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>

基本上,我需要的是,在为 tbl_NIR 创建新行时(例如:ID = 23 (自动递增),等等),ID 的值(“23”)显示在部分视图上:

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

字段。我认为可能是“EdidorFor是问题”!?

(注:此处的拼写错误已经被保留)

你的 PartialView 在哪里? - Mihai Alexandru-Ionut
1个回答

2

您不能重定向到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# - 传递一个新的 BOL1.tbl_I_O 对象。

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

HTML

更改

@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"]"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接