ActionResult为MVC4中的动态列表更新db

时间:2013-10-26 20:43:06

标签: c# asp.net-mvc-4 razor edmx actionresult

我需要在我的控制器中使用Action结果来保存用户的内联版本。我成功使用来自http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx的源码从edmx创建列表,但是当我保存时我不知道如何处理actionresult。

请参阅我的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>

1 个答案:

答案 0 :(得分:0)

您说要更新列表,但您的操作链接指向创建方法。如果您正在执行更新,则需要将actionlink的操作指向执行更新的方法。如果您需要一个例子,请告诉我。

您需要在控制器中以POST方法返回模型。如果您保留默认的脚手架,它应该已经存在。

应该看起来像这样......

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

第一种方法是GET方法。第二种是POST方法,即点击提交按钮时将调用的方法。

第二种方法返回模型对象的默认视图。如果我想要返回不同的视图,我会指定这样的视图......

返回查看(“_ myotherview”,预订);

“内联编辑”是什么意思?