ASP.NETMVC5如何将主从视图传递给控制器?
创始人
2024-09-19 03:01:01
0
  1. 在View中,使用表单元素将主从视图的数据绑定到一个视图模型中,将视图模型传递给控制器。如下示例:

View视图代码:

@model MyViewModel

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{
    @Html.LabelFor(model => model.MasterProperty)
    @Html.EditorFor(model => model.MasterProperty)
    
    // Detail视图表格
    
        @foreach(var detail in Model.DetailList)
        {
            
        }
    
@Html.LabelFor(model => detail.DetailProperty1, detail.Id.ToString()) @Html.HiddenFor(model => detail.Id) @Html.EditorFor(model => detail.DetailProperty1) @Html.ValidationMessageFor(model => detail.DetailProperty1) @Html.LabelFor(model => detail.DetailProperty2) @Html.EditorFor(model => detail.DetailProperty2) @Html.ValidationMessageFor(model => detail.DetailProperty2)
}

ViewModel代码:

public class DetailViewModel
{
    public int Id { get; set; }
    public string DetailProperty1 { get; set; }
    public string DetailProperty2 { get; set; }
}

public class MyViewModel
{
    public string MasterProperty { get; set; }
    public List DetailList { get; set; }
}
  1. 在控制器中,使用视图模型来接受主从视图的数据。如下示例:

Controller代码:

public class MyController : Controller
{
    [HttpPost]
    public ActionResult ActionName(MyViewModel viewModel)
    {
        // Save MasterData
        var masterData = new MasterDataModel
        {
            MasterProperty = viewModel.MasterProperty
        };
        // Save DetailData
        foreach (var detail in viewModel.DetailList)
        {
            var detailData = new DetailDataModel
            {
                MasterId = masterData.Id,
                DetailProperty1 =

相关内容

热门资讯

透视真的(德州之星插件)微扑克... 透视真的(德州之星插件)微扑克德州专用辅助器游戏平台(详细辅助揭秘教程)一贯真的有挂1、很好的工具软...
透视软件(wepOKE)外挂透... 您好,wepoke模拟器这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家...
辅助透视(德州微扑克辅助)微扑... 辅助透视(德州微扑克辅助)微扑克真的有外挂嘛(详细辅助科技教程)其实真的有挂;致您一封信;亲爱德州微...
透视教程(wepoKE)透视辅... 1、透视教程(wepoKE)透视辅助app(wepoke智能ai)起初是有挂(详细透视规律教程)2、...
透视免费(wepokeai机器... 透视免费(wepokeai机器人)aapoker外挂实测(详细辅助透视教程)其实是真的有挂,您好,w...
透视数据(WepokE)外挂透... 透视数据(WepokE)外挂透明挂辅助app(wepoke真的有挂)确实真的是有挂(详细透视可靠技巧...
透视了解(德州之星辅助)微扑克... 透视了解(德州之星辅助)微扑克算正规平台吗(详细辅助安装教程)好像真的有挂1、构建自己的微扑克辅助插...
透视安卓版(wepOkE)外挂... 透视安卓版(wepOkE)外挂透明挂辅助app(wepoke辅助德之星)果然有挂(详细透视力荐教程)...
透视透视(wepoke ai辅... 自定义wepoke ai辅助系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器...
透视能赢(wepOke)外挂透... 1、透视能赢(wepOke)外挂透明挂辅助神器(wepoke有挂)一向真的是有挂(详细透视2025新...