c# - 使用 ASP.NET MVC 中父页面模型的内容填充 PartialView 模型

标签 c# asp.net asp.net-mvc razor model

我正在尝试弄清楚如何填充局部 View 的模型。例如,PartialView 模型包括用户在注释对话框中输入的所有内容,例如主题和注释内容,但它还需要来自父页面的模型的唯一 ID,即产品 ID。这样,当从对话窗口保存注释时,我可以为特定产品保存它。

在下面的示例中,如果用户在 ParentPage 上查看 ID 为 12 的产品并想要添加注释,他们将单击注释按钮打开对话框窗口,填写主题和内容并点击提交。在提交给 Controller 的过程中,如果 ProductId 为 12,EntityModule 应该 = "Product"并且 EntityKey 应该 = 12。我想弄清楚 NoteViewModel 如何从父 ViewModel 检索这两个字段。

ProductViewModel.cshtml

public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }

NoteViewModel.cshtml

public int Id { get; set; }
public string EntityModule { get; set; }
public int EntityKey { get; set; }
public string Subject { get; set; }
public string Content { get; set; }

ParentPage.cshtml

@model MyApp.Web.ViewModels.Shared.ProductViewModel
@{ await Html.RenderPartialAsync("_NotesPartial"); }

_NotesPartial.cshtml

@model MyApp.Web.ViewModels.Shared.NoteViewModel
<button id="open" class="btn btn-primary">Notes</button>

@(Html.Kendo().Window()
    .Name("window")
    .Title("About Alvar Aalto")
    .Visible(false)
    .Draggable()
    .Resizable()
    .Width(600)
    .Actions(actions => actions.Pin().Minimize().Maximize().Close())
    .Content(@<text>
    <form asp-action="_NotesPartial">
        <div class="form-horizontal">
            <h4>NoteViewModel</h4>
            <hr />
            <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Subject" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <input asp-for="Subject" class="form-control" />
                    <span asp-validation-for="Subject" class="text-danger" />
                </div>
            </div>
            <div class="form-group">
                <label asp-for="Content" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <textarea asp-for="Content" class="form-control" cols="40" rows="4"></textarea><span asp-validation-for="Content" class="text-danger" />
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    </form>
    </text>)
)

<script>
    $("#open").click(function () {
        var win = $("#window").data("kendoWindow");
        win.center();
        win.open();
    });
</script>

最佳答案

其中一种方式是通过父页面填充和传递模型。例如:

public class ProductViewModel {
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public NoteViewModel Note { get; set;}
}

然后,使用这样的东西:

@{ await Html.RenderPartialAsync("_NotesPartial", Model.Note); }

MVC6 中没有 RenderAction,这是一个很好的解决方案:

Html.RenderAction("_NotesPartial", new { id = ProductViewModel.Id })

但您仍然可以在 MVC5 中使用它。

关于c# - 使用 ASP.NET MVC 中父页面模型的内容填充 PartialView 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35116781/

相关文章:

ajax - 在 asp.mvc 中使用 history api 处理直接 url

c# - 处理鼠标右键双击形状

c# - 如何忽略 ASP.NET MVC 路由中 "controller/action"之后的所有字符?

c# - 在异步代码中使用 BinaryWriter 或 BinaryReader

c# - 如何从纯文本解析 C# 中的存储过程签名

c# - 增加 ASP.NET 页面方法的最大响应大小

asp.net - 如何以编程方式从 ASP.NET 代码隐藏提供 HTTP 404 服务并重定向到 404 页面?

asp.net-mvc - 本地化静态文本asp.net mvc3。方法是什么?

c# - 如何使用来自 mssql 数据库的数据单击 C# .net 页面中包含的 JavaScript block 内的每个标记来添加信息窗口

asp.net - 在 iis 5 上托管 ASP.NET MVC 时找不到页面错误