HttpPost 创建 MVC

发布于 2024-10-21 09:06:13 字数 609 浏览 3 评论 0原文

我有以下三个类:

public class AModel { .. } 

public class BModel { 
.. 
public List A { get; set; } 
} 

public class CModel { 
.. 
public List B { get; set; } 
}

如何获取CModel控制器的httpPost Create中的List对象中的数据?

使用编辑器模板:

// CModel
@Html.EditorFor(model => model.B) 
<p>
    <input type="submit" value="Create" />
</p>

// BModel
@Html.EditorFor(model => model.A) 
<p>
    <input type="submit" value="Create" />
</p>

它可以与 List 配合使用,但我无法访问 httpPost create 方法中的 List 对象。

提前致谢,

布鲁诺·格朗让

I have the following three classes:

public class AModel { .. } 

public class BModel { 
.. 
public List A { get; set; } 
} 

public class CModel { 
.. 
public List B { get; set; } 
}

How can I get the data in List objects in the httpPost Create of CModel Controller?

with the editor templates:

// CModel
@Html.EditorFor(model => model.B) 
<p>
    <input type="submit" value="Create" />
</p>

// BModel
@Html.EditorFor(model => model.A) 
<p>
    <input type="submit" value="Create" />
</p>

it works fine with List but I cannot access to the List objects in the httpPost create method.

thks in advance,

bruno grandjean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2024-10-28 09:06:13

如果你有这样的事情

public class AModel { .. } 

public class BModel { 
.. 
public List<A>  AList { get; set; } 
} 

public class CModel { 
.. 
public List<B> BList { get; set; } 
}

你应该能够做到

CModel model;
var blist model.BList;

foreach(var b in blist)
{
    var alist = b.Alist;
}

If you have something like this

public class AModel { .. } 

public class BModel { 
.. 
public List<A>  AList { get; set; } 
} 

public class CModel { 
.. 
public List<B> BList { get; set; } 
}

you should be able to do

CModel model;
var blist model.BList;

foreach(var b in blist)
{
    var alist = b.Alist;
}
橘亓 2024-10-28 09:06:13

非常感谢您这么快回复我。我终于找到了解决方案,这是我的类定义:

public partial class ClasseAmodel
{
    public int id { get; set; }
    public string label { get; set; }
}
public partial class ClasseBmodel
{
    public List<ClasseAmodel> classeA { get; set; }
}
public partial class ClasseCmodel
{        
    public List<ClasseBmodel> classeB { get; set; }
}

我的 ClasseCmodel 创建视图:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" 
    type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" 
    type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Create</legend>
        @Html.EditorFor(Model => Model.classeB)
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

我的 EditorTemplates:

@model MetaCrud.Models.ClasseAmodel

<p>
    @Html.LabelFor(model => model.id)
    @Html.TextBoxFor(model => model.id)
    @Html.ValidationMessageFor(model => model.id)
</p>
<p>
    @Html.LabelFor(model => model.label)
    @Html.TextBoxFor(model => model.label)
    @Html.ValidationMessageFor(model => model.label)
</p>

@model MetaCrud.Models.ClasseBmodel

<p>
       @Html.EditorFor(Model => Model.classeA)
</p>

我的 ClasseCmodelController 创建方法:

// GET: /ClasseCmodel/Create
public ActionResult Create()
{
    var model = new ClasseCmodel();
    classeB = new List<ClasseBmodel>();
    var b = new ClasseBmodel();
    b.classeA = new List<ClasseAmodel>();
    var a = new ClasseAmodel();
    a.id = 1;
    a.label = "1";
    b.classeA.Add(a);
    a = new ClasseAmodel();            
    a.id = 2;
    a.label = "2";
    b.classeA.Add(a);
    classeB.Add(b);
    classeC = new List<ClasseCmodel>();

    return View(model);
} 

// POST: /ClasseCmodel/Create
[HttpPost]
public ActionResult Create(ClasseCmodel model, FormCollection collection)
{
    return View(model);
}

在最后一个方法中,我恢复了整个对象集,特别是 ClasseAmodel 对象:

id = 1;
label = "1";

id = 2;
label = "2";

是我的主要目标..
更有趣的是,我这样修改了我的 ClasseCModel:

public partial class ClasseCmodel
{        
    public List<ClasseCmodel> classeC { get; set; }
}

以便构建一个递归列表。我为 ClasseCmodel 创建了一个编辑器模板:

<p>
       @Html.EditorFor(Model => Model.classeC)
</p>

并且刚刚添加:

@Html.EditorFor(Model => Model.classeC)

在 ClasseCmodel 创建视图中。
我可以在没有 pb 的情况下在 classeC 列表中添加 ClasseCmodel 对象,现在我得到了一个递归视图。

Thks a lot for replying to me so quickly. I finally found the solution, here is my classes definition:

public partial class ClasseAmodel
{
    public int id { get; set; }
    public string label { get; set; }
}
public partial class ClasseBmodel
{
    public List<ClasseAmodel> classeA { get; set; }
}
public partial class ClasseCmodel
{        
    public List<ClasseBmodel> classeB { get; set; }
}

My ClasseCmodel create view :

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" 
    type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" 
    type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Create</legend>
        @Html.EditorFor(Model => Model.classeB)
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

My EditorTemplates :

@model MetaCrud.Models.ClasseAmodel

<p>
    @Html.LabelFor(model => model.id)
    @Html.TextBoxFor(model => model.id)
    @Html.ValidationMessageFor(model => model.id)
</p>
<p>
    @Html.LabelFor(model => model.label)
    @Html.TextBoxFor(model => model.label)
    @Html.ValidationMessageFor(model => model.label)
</p>

@model MetaCrud.Models.ClasseBmodel

<p>
       @Html.EditorFor(Model => Model.classeA)
</p>

My ClasseCmodelController create methods :

// GET: /ClasseCmodel/Create
public ActionResult Create()
{
    var model = new ClasseCmodel();
    classeB = new List<ClasseBmodel>();
    var b = new ClasseBmodel();
    b.classeA = new List<ClasseAmodel>();
    var a = new ClasseAmodel();
    a.id = 1;
    a.label = "1";
    b.classeA.Add(a);
    a = new ClasseAmodel();            
    a.id = 2;
    a.label = "2";
    b.classeA.Add(a);
    classeB.Add(b);
    classeC = new List<ClasseCmodel>();

    return View(model);
} 

// POST: /ClasseCmodel/Create
[HttpPost]
public ActionResult Create(ClasseCmodel model, FormCollection collection)
{
    return View(model);
}

In the last method I recover the whole set of my objects and in particularly the ClasseAmodel objects :

id = 1;
label = "1";

and

id = 2;
label = "2";

which was my main goal..
More interesting, I modified my ClasseCModel like that :

public partial class ClasseCmodel
{        
    public List<ClasseCmodel> classeC { get; set; }
}

So as to build a recursive List. I created an editor template for ClasseCmodel :

<p>
       @Html.EditorFor(Model => Model.classeC)
</p>

And just added :

@Html.EditorFor(Model => Model.classeC)

In the ClasseCmodel create view.
I can add ClasseCmodel objects in the classeC list without pb and I get now a recursive view..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文