1

In my Controller I have two Actions, the POST action is called when a combo is changed or the update button is pressed. This is working correctly.

When I do a Create, I use a return RedirectToAction("Index") if the create was successful.

Here is where the problem starts. I need that the combo in the index get the selectedvalue of the id used in the create, but I cannot pass another parameter in the Index action, because the POST Action already has one.

How can I pass a parameter to the GET Action and update the combo, with the selectedvalue from the Create Action?

I tried to use a "Return View()", it worked but not as expected.

These are the Actions:

[HttpGet]
public ActionResult Index()
{
    ViewBag.ID_CONTRATO = new SelectList(db.CAT_CONTRATOS.Where(t => (t.BAJA == null || t.BAJA == false)), "ID_CONTRATO", "CONTRATO");
    ViewBag.listado = Enumerable.Empty<CAT_CONCEPTOS>();
    return View();
}

[HttpPost]
public ActionResult Index(int Contrato = 0)
{
    if (Contrato == 0)
    {
        ViewBag.ID_CONTRATO = new SelectList(db.CAT_CONTRATOS.Where(t => (t.BAJA == null || t.BAJA == false)), "ID_CONTRATO", "CONTRATO");
        ViewBag.listado = Enumerable.Empty<CAT_CONCEPTOS>();
    }
    else
    {
        ViewBag.ID_CONTRATO = new SelectList(db.CAT_CONTRATOS.Where(t => (t.BAJA == null || t.BAJA == false)), "ID_CONTRATO", "CONTRATO", Contrato);
        var cat_conceptos = db.CAT_CONCEPTOS.Include(c => c.CAT_CONTRATOS).Where(t => ((t.BAJA == null || t.BAJA == false) && (t.ID_CONTRATO == Contrato)));
        ViewBag.listado = cat_conceptos;
    }
    return View();
}

[HttpPost]
public ActionResult Create(CAT_CONCEPTOS cat_conceptos)
{
    if (ModelState.IsValid)
    {
        using (TransactionScope scope = new TransactionScope())
        {
            db.CAT_CONCEPTOS.Add(cat_conceptos);
            db.SaveChanges();
            TransaccionesController t = new TransaccionesController();
            t.Transaccionar("CAT_CONCEPTOS", "Create", cat_conceptos.ID_CONCEPTO, "Se creo un concepto");

            scope.Complete();
            return RedirectToAction("Index");
        }
    }

    ViewBag.ID_CONTRATO = new SelectList(db.CAT_CONTRATOS.Where(t => (t.BAJA == null || t.BAJA == false)), "ID_CONTRATO", "CONTRATO", cat_conceptos.ID_CONTRATO);
    return View(cat_conceptos);
}

And the View:

@model IEnumerable<SAC.Models.CAT_ACTIVOS>
@{
string controlador = ViewContext.RouteData.Values["Controller"].ToString();    
}
@section head{
<link href="@Styles.Url("~/content/DataTables/css")" rel="stylesheet"/>
}
@{
ViewBag.Title = "Activos";
}

<h2>@ViewBag.Title</h2>

<div class="row-fluid">
<div class="span12">
    <div class="row-fluid">
        @using (Html.BeginForm("Index", "Activos"))
        {
            <div class="span9">
                @Html.DropDownList("Contrato", (SelectList)ViewBag.ID_CONTRATO, "Seleccione un Contrato", new { @class = "span12", onchange = @"var form = document.forms[0]; form.submit();" })
            </div>
            <div class="span1">
                <button type="submit" class="btn btn-danger span12"><i class="icon-refresh"></i></button>
            </div>
            <div class="span2">
                @Html.ActionLink("Agregar", "Create", @controlador, new { @class = "btn btn-danger span12" })
            </div>
        }
    </div>
</div>
</div>
@Html.Partial("Shared/_GridActivos", (IEnumerable<SAC.Models.CAT_ACTIVOS>)ViewBag.listado)

@section JavaScript{
@Scripts.Render("~/Scripts/DataTables/js")
}
4

1 回答 1

1

这是 TempData 派上用场的情况之一,作为在重定向期间存储该信息的一种方式。

TempData["MyId"] = t.ID;
return RedirectToAction("Index");

然后,在您的索引操作中:

if (TempData["MyId"] != null) {
    // Retrieve ID and object from the database to load the model.
} else {
    // Your existing code.
}

根据模型的大小和可序列化性,您甚至可以将创建的模型对象存储在 TempData 中并在 Index.xml 中检索它。

于 2013-04-19T15:49:29.583 回答