How to call method on the Controller from view

First of all I'm a little new on this and I have a lot of questions but one of them is the next one. Is there any way to call methods (without view) situated on the Controller from another view and send one parameter at the same time? I've been trying to do it but I can't. I have tried this:

@{
  ((HomeController)this.ViewContext.Controller).Method1();
}

but I get an error which says that namespace could not be found the flow is: open the view->click on add button->select one row from other table->through coding, send the Id of the product to the method I need in order to find a product and insert it into my new table.

here you have some code.

First View

<div id="lista" class="modal fade" role="dialog">
    <div class="modal-dialog">


        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">

                @{Html.RenderAction("selectOrden", "Producto");}
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
            </div>
        </div>

    </div>
</div>

Controller

    [HttpGet]
        public ActionResult selectOrden()
        {
            try
            {
                client = new FireSharp.FirebaseClient(config);
                FirebaseResponse response = client.Get("Producto");
                dynamic data = JsonConvert.DeserializeObject<dynamic>(response.Body);
                var list = new List<Producto>();
                foreach (var item in data)
                {
                    list.Add(JsonConvert.DeserializeObject<Producto>(((JProperty)item).Value.ToString()));
                    Debug.WriteLine(JsonConvert.DeserializeObject<Producto>(((JProperty)item).Value.ToString()).nombreProducto);
                    return View(list);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }
            return View();
        }

        //Inserta registros de productos en las ordenes de compra

        [HttpPost]
        public void selectOrdena(string id)
        {
            client = new FireSharp.FirebaseClient(config);
            FirebaseResponse responseProdu = client.Get("Producto/" + id);
            Producto dataProdu = JsonConvert.DeserializeObject<Producto>(responseProdu.Body);
            ViewBag.Soli = dataProdu;


            Debug.WriteLine("Hello");

            //return View(dataProdu);
        }

Second View

    @model IEnumerable<FirebaseMVCTestApp.Models.Producto>
    
    @{
        ViewBag.Title = "selectOrden";
        Layout = null;
    }
    
    <h2>selectOrden</h2>
    
    @using (Html.BeginForm("Save","ProductoController", FormMethod.Post))
    {
    
        <form method="post">
            @Html.AntiForgeryToken()
    
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <table id="table_idxd" class="display">
                <thead>
                    <tr>
                        <th>
                            Nombre del producto
                        </th>
                        <th>
                            Cantidad
                        </th>
                        <th>
                            Costo
                        </th>
                        <th>
                            Precio
                        </th>
                        <th>
                            Codigo
                        </th>
                        <th>Acción</th>
                    </tr>
                </thead>
                @{ try
                    {
                        foreach (var item in Model)
                        {
                            <tr>
    
                                <td>
    
                                    @Html.EditorFor(model => item.nombreProducto, new { htmlAttributes = new { @class = "form-control" } })
    
                                </td>
                                <td>
                                    @Html.EditorFor(modelItem => item.cantidad, new { htmlAttributes = new { @class = "form-control" } })
    
                                </td>
                                <td>
                                    @Html.EditorFor(modelItem => item.costo, new { htmlAttributes = new { @class = "form-control" } })
    
                                </td>
                                <td>
                                    @Html.EditorFor(modelItem => item.precio, new { htmlAttributes = new { @class = "form-control" } })
    
                                </td>
                                <td>
                                    @Html.EditorFor(modelItem => item.codigo, new { htmlAttributes = new { @class = "form-control" } })
    
                                </td>
                                <td>
                                    @Html.ActionLink("Agregar", "selectOrdena", new { id = item.idProducto })
                                    <input type="submit" value="Save" class="btn btn-default" />
                                   
                                </td>
                            </tr>
                        }
                    }
                    catch (Exception ex)
                    {
    
                    }
    
    
                }
            </table>
        </form>
    
    
    
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
    
            </div>
        </div>
    
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

Upvotes: 0

Views: 1173

Answers (2)

Ok I've checked all the other threads and I think it was going so far from my problem, but I have already solved it by adding this line on the second view

@using FirebaseMVCTestApp.Controllers

in that way the view could see the controller I needed.

I know that the question was a little newbbie but if I didn't ask I would probably never find the answer.

Thank you. :)

Upvotes: 0

Tupac
Tupac

Reputation: 2932

The method you provided is valid. This is how you call an instance method on the Controller:

@{
  ((HomeController)this.ViewContext.Controller).Method1();
}

This is how you call a static method in any class:

@{
    SomeClass.Method();
}

This will work assuming the method is public and visible to the view.But according to what you said, an error was reported:

that namespace could not be found

The reason may be that the project that references the assembly may have a different frame type from the assembly.You can check this post, it may be helpful to you:https://stackoverflow.com/questions/4764978/the-type-or-namespace-name-could-not-be-found

Upvotes: 1

Related Questions