一个视图中的多个表单相同的操作

时间:2017-08-19 13:22:42

标签: c# asp.net asp.net-mvc

我在使用多个表单创建视图时遇到问题,在同一个视图中我可以获取数据视图的操作和数据收集的后期操作。

以下是我的主要观点:



@model Models.BuildingNewBuildingViewModel
@{
    ViewBag.Title = "Index";
}
@if (TempData["Added"] != null)
{
    <div class="alert alert-success alert-dismissible fade in" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">×</span>
        </button>
        @TempData["Added"]
    </div>

}
@if (TempData["AddError"] != null)
{
    <div class="alert alert-danger alert-dismissible fade in" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">×</span>
        </button>
        @TempData["AddError"]
    </div>

}
<h1>Svěřené budovy pro: @ViewBag.Name</h1>




<table class="table-bordered table-responsive table table-condensed">
    <thead>
        <tr>
            <th>Id budovy</th>
            <th>Podlaží</th>
            <th>Použití</th>
            <th>Výměra</th>
            <th>Datum</th>
            <th>Administrace</th>

        </tr>
    </thead>
    <tbody>



        @for (int i = 0; i < Model.NewBuildingViewModels.Count; i++)
        {
            using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal", id = "Form" }))
            {

                Html.RenderPartial("IndexPartial", Model.NewBuildingViewModels[i]);
            }

        }



    </tbody>


</table>
&#13;
&#13;
&#13;

这是表格:

&#13;
&#13;
@model Models.NewBuildingViewModel
    

    @Html.HiddenFor(x => x.Building, Model.Building)
    <tr>
        <td><a href="@Url.Action("SetMarks", "Home", new {id = Model.Id})">@Model.Building</a></td>
        <td>
            @if (Model.Floor > 0)
            {
                @Model.Floor
                @Html.HiddenFor(x => x.Floor)
            }
            else
            {
                @Html.TextBoxFor(x => x.Floor, new {@Value = ""})
                @Html.ValidationMessageFor(x => x.Floor)

            }
        </td>
        <td>
            @if (Model.Usage != null)
            {
                @Model.Usage
                @Html.HiddenFor(x => x.Usage)

            }
            else
            {
                @Html.TextBoxFor(x => x.Usage)
                @Html.ValidationMessageFor(x => x.Usage)
            }
        </td>
        <td>
            @if (Model.Size > 0)
            {
                @Model.Size
                @Html.HiddenFor(x => x.Size)

            }
            else
            {
                @Html.TextBoxFor(x => x.Size, new {@Value = ""})
                @Html.ValidationMessageFor(x => x.Size)
            }
        </td>
        <td>
            @if (Model.Date != DateTime.MinValue)
            {
                @Model.Date.Date
                @Html.HiddenFor(x => x.Date)

            }
            else
            {
                @Html.TextBoxFor(x => x.Date)
                @Html.ValidationMessageFor(x => x.Date)

            }
        </td>

        <td>
            @if (!Model.NewBuildingDataInDatabase)
            {
                <button type="submit">Odeslat</button>
            }
            else
            {
                if (Model.MarksInDatabase)
                {
                    <i class="fa fa-check checkIcon" aria-hidden="true"></i>
                }
                else
                {
                    <a href="@Url.Action("SetMarks", "Home", new {id =   Model.Id})">Zadat Hodnocení</a>
                }

            }

        </td>
    </tr>
}
&#13;
&#13;
&#13;

我的问题是当我向控制器发送无效数据时。验证消息显示所有表单。我想查看所有表单,但仅查看已发送数据的表单的验证消息。

这是我的行动:

 [HttpPost]
    public ActionResult Index(NewBuildingViewModel buildingNewBuildingViewModel)
    {
        BuildingForIcoCommand buildingForIcoCommand = new BuildingForIcoCommand();
        var buildingsForIco = buildingForIcoCommand.GetBuildingListForIco(AppContext);

        if (ModelState.IsValid)
        {
            ViewBag.Name = User.Identity.Name;
            ViewModelBuilder viewModelBuilder = new ViewModelBuilder();

            DaoBase<NewBuilding, int> daoBase = new DaoBase<NewBuilding, int>(AppContext.NhSession);
            daoBase.Save(viewModelBuilder.CreateNewBuilding(buildingNewBuildingViewModel));
            return View(buildingForIcoCommand.GetBuildingListForIco(AppContext));
        }

        return View("Index",buildingsForIco);
    }

1 个答案:

答案 0 :(得分:0)

您的所有表单字段都将具有name和id属性,因为您在部分字段中呈现它们。这就是为什么它们都被标记为无效。

任何好的解决方案都可能涉及ajax。

解决此问题的一种方法是使用单个编辑表单。每个呈现的项目都有一个编辑按钮,单击此按钮将通过javascript使用项目的数据填充表单。这可能是模态或类似的。这种方法适用于jQuery / unobtrusive验证。

另一种方法是从控制器返回自定义响应,例如状态400和从模型状态派生的无效字段。然后,您可以在右侧字段中显示合适的错误消息。

相关问题