Question

I need an Action result in my controller that saves inline edition from the user. I succeded in create the list from edmx using the source from http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx, but when i save i dont know what to do with the actionresult.

See my cstml:

    @model List<SCP___AgroGerente.Models.VeiculoFazendaUsuario>

@{
    ViewBag.Title = "Index";
}

<div class="Cabecalho">
    <div class="left">
        <h2>Lista de Veículos</h2>
        <h4>Aqui você cadastra as Veículos</h4>
    </div>
    <div class="right" style="padding-top: 28px">

        @Html.ActionLink(" ", "Create", string.Empty, new { @class = "icone new" })

    </div>
    <div class="clear"></div>

    <hr />
</div>
<table class="tabelaFormatada">
    <tr>
        <th>Especificação
        </th>
    </tr>
    @using (Html.BeginForm())
    {
        for (int i = 0; i < Model.Count(); i++)
        {

        <tr>
            <td>
                @Html.EditorFor(m => Model[i].VeiculoEspecificacao);
            </td>
        </tr>
        }
        <p>
            <input type="submit" value="Salvar Alterações" />
        </p> }
</table>
Was it helpful?

Solution

You say you want to update the list, but your action link is pointing to a create method. If you're performing an update, you need to point the action of the actionlink to a method that performs an update. Let me know if you need an example.

You need to return the model in a POST method in your controller. If you kept the default scaffolding, it should already be there.

It should look something like this...

    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /booking/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(booking booking)
    {
        if (ModelState.IsValid)
        {
            db.bookings.Add(booking);
            db.SaveChanges();
        }

        return View(booking);
    }

The first method is the GET method. The second is the POST method, which is what will be called when you hit the submit button.

The second method returns the default view with the model object. If I wanted a different view to be returned, I'd specify that view like this...

return View("_myotherview", booking);

What do you mean by "inline editiing"?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top