使用相同的partial将表单添加到“创建”和“编辑”操作

时间:2016-09-15 19:08:38

标签: asp.net-mvc asp.net-mvc-5

在VS上创建新的脚手架项目时,它会创建几乎完全相同的“创建”和“编辑”操作的视图,但“主要”键的“编辑”视图的@Html.HiddenFor除外。

编辑视图示例:

@model MyApp.Models.Destaque

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(m => m.IdDestaque)
        <div class="form-group">
            @Html.LabelFor(model => model.Mensagem, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Mensagem, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Mensagem, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CaminhoImagem, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CaminhoImagem, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CaminhoImagem, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.UrlLink, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UrlLink, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UrlLink, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Salvar" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

如果我放置BeginForm中的所有内容(包括@using (...)并将@Html.HiddenFor(m => m.IdDestaque)保留在_Form partial中,则不允许我创建新行。

如果我从_Form partial中删除@Html.HiddenFor,则编辑操作不起作用(ModelState无效)。

那么做到这一点并保持DRY原则的正确方法是什么?在我的编辑操作中从ModelState验证中删除PK似乎是一个“uggly”解决方法。

1 个答案:

答案 0 :(得分:0)

您应该在视图中呈现模型的id,并为Html.BeginForm使用另一个重载,如下所示:

@using (Html.BeginForm("Edit", "Controller", FormMethod.Post, new{attr1 = value1, attr2 = value2}))
{
    @Html.HiddenFor(x => x.Id)
    ...
}

您总是在 EditAction 上发帖。在您的控制器中,您只需要一个POST操作用于编辑,两个获取操作一个创建和其他编辑。

public ActionResult Create()
{
   return View("Edit"); //return the edit view
}

public ActionResult Edit(int id)
{
    var entity = manager.FetchById(id);
    if (entity == null)
       return HttpNotFound();

    //convert your entity to model (optional, but recommended)
    var model = Mapper.Map<ViewModel>(entity);

    //return your edit view with your model
    return View(model);
}

[HttpPost]
public ActionResult Edit(ViewModel model)
{
    if (ModelState.IsValid)
    {
        //work with your model

        return RedirectToAction("Index", "Controller");
    }

    //at this point there is an error, so your must return your view
    return View(model);
}

希望这有帮助!