如何在编辑视图中创建局部视图?

时间:2016-09-21 12:41:07

标签: c# asp.net-mvc entity-framework razor bootstrap-modal

我可以在模态引导程序中添加创建局部视图,以便它可以使用字段值的编辑视图在表中添加值吗?

我的创建局部视图

@model MVCLayout.Models.Table2

    <p>
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            <div class="form-group">
                @Html.Label("Cargo: ", new { style = "width:160px" })
                @Html.TextBoxFor(model => model.ID, new { style = "width:200px" })
            </div>
            <br/>

            <div class="form-group">
                @Html.Label("Percentual: ", new { style = "width:160px" })
                @Html.TextBoxFor(model => model.Name, new { style = "width:200px" })
            </div>
            <br/>

            <div class="form-group">
                @Html.Label("Serviço: ", new { style = "width:160px" })
                @Html.TextBoxFor(model => model.Work, new { style = "width:200px" })
            </div>
            <br/>

            <p>
               <input type="submit" value="Save" />
           </p>

        }

        </p>

我的编辑视图

<html>
<body>
@model MVCLayout.Models.Table1

@{
    ViewBag.Title = "Edit";
}
    <div id="signup">
        <div class="rontainer">
            <div class="header">
                <div id="dock">
                <br>
                @using (Html.BeginForm("Edit", "AdmServicos", FormMethod.Post, new { @class = "form-inline" }))
                {
                    @Html.AntiForgeryToken()

                    <fieldset>
                        <legend>Editar Servios</legend>
                        <br>
                        <div class="form-group">

                            @Html.Label("Código:", new { style = "width:160px" })
                            @Html.TextBoxFor(model => model.ID, new { style = "width:55px", @readonly = "readonly" })

                        <div class="form-group">
                            @Html.Label("Descrição: ", new { style = "width:160px" })
                            @Html.TextBoxFor(model => model.Descricao, new { style = "width:550px", @readonly = "readonly" })
                        </div>


                        </div>

                        <br />
                        <br /><br />
                        <p>
                            <input type="submit" value="Salvar" />
                        </p>
                    </fieldset>
                    <div class="modal fade" id="myModal" role="dialog">
                        <div class="modal-dialog">

                            <!-- Modal content-->
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                    <h4 class="modal-title">Modal Header</h4>
                                </div>
                                <div class="modal-body">
                                    @{
                                        Html.RenderPartial("CreateTable2", Model.ID);
                                    }
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                </div>
                            </div>

                        </div>
                    </div>
                                        }
            </div>
        </div>
    </div>
</body>
</html>

我可以使用部分视图或最佳方式执行此操作吗?

1 个答案:

答案 0 :(得分:1)

在Table1模型中创建Table2类型的属性,如下所示:

public class Table1
{
  public Table2 table2 {get; set;}

//other properties of Table1 Model
}

现在在编辑视图中:

 <div class="modal-body">
        @{

      Html.RenderPartial("CreateTable2", Model.table2);
          }
       </div>

现在进入你的控制器行动:

public ActionResult Edit(Table1 model)
{
var editPartialViewData = model.table2;
// Do whatever you want to do with this data

}