如何在编辑视图中创建部分视图

本文关键字:视图 创建部 编辑 | 更新日期: 2023-09-27 18:08:00

我可以在模态引导中添加一个创建部分视图,以便它可以在表中添加值与字段值的编辑视图

My create partial view

@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>

My Edit View

<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>

我可以用局部视图还是最好的方法来做到这一点?

如何在编辑视图中创建部分视图

在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
}