MVC3 Madness - 请帮助绑定包含的集合?

发布于 2024-12-11 12:45:26 字数 1051 浏览 0 评论 0原文

我在这里完全不知所措,找不到解决这个问题的方法。有人可以帮忙提供一些代码:

  • 允许使用 [] 以便保存 @model 中包含的集合吗?
  • 以某种方式从模型中映射/绑定包含的集合(我已阅读 Phil Haack 的集合绑定博客,但这不是传入的平面集合....我已经有一个模型 A 进来?
  • 我也尝试过设置一个新的视图模型(包含下面的模型类 A 和 B),但我的模型在 httppost 中返回 null(即使我添加一个简单的字符串类型,如“Name”并绑定它......返回 null)。我确信存在我不知道的 AutoMapper 问题 我是mvc3

的新手,详细信息如下...

编辑视图:

    @model MVC3.Models.A

   // I need to save collection values but can't use [] here to setup model binding.
   // I have read about mapping collections but I already have a model A that is getting passed in.
   //
   @Html.EditorFor(model => model.Bs[0].Val)

模型:

public  class A
{
    public A()
    {
        this.Bs = new HashSet<B>();
    }

    public int Name { get; set; }
    public virtual ICollection<B> Bs { get; set; }  // Can't change this to ILIst because of above HashSet

 - }

       public  class B
       {
           public int Val { get; set; }  
           public virtual A A { get; set; }
       }

I am totally at a loss here and can't find a solution to this problem. Can somebody please help provide some code to either:

  • Allow [] to be used so as to save contained collection within a @model?
  • Somehow map/bind a contained collection from within a model (I have read Phil Haack's Collection binding blog but this isn't a flat collection being passed in....I already have a model A coming in?
  • I have also tried setting up a new view model (contains model classes A and B from below) but my model is coming back null in the httppost (even if I add a simple string type like "Name" and bind it...comes back null). I'm sure there AutoMapper issues that I am not aware of.

I'm a complete newbie with mvc3. Here are the details...

Edit View:

    @model MVC3.Models.A

   // I need to save collection values but can't use [] here to setup model binding.
   // I have read about mapping collections but I already have a model A that is getting passed in.
   //
   @Html.EditorFor(model => model.Bs[0].Val)

Models:

public  class A
{
    public A()
    {
        this.Bs = new HashSet<B>();
    }

    public int Name { get; set; }
    public virtual ICollection<B> Bs { get; set; }  // Can't change this to ILIst because of above HashSet

 - }

       public  class B
       {
           public int Val { get; set; }  
           public virtual A A { get; set; }
       }

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

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

发布评论

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

评论(1

沫尐诺 2024-12-18 12:45:26

您的模型在视图模型中具有循环引用。这不是默认模型绑定器支持的场景。我建议您始终在视图中使用编辑器模板。示例:

模型:

public  class A
{
    public int Name { get; set; }
    public virtual ICollection<B> Bs { get; set; }
}

public class B
{
    public int Val { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new A
        {
            Name = 123,
            Bs = new[]
            {
                new B { Val = 1 },
                new B { Val = 2 },
                new B { Val = 3 },
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(A a)
    {
        // The Bs collection will be properly bound here
        return View(a);
    }
}

视图(~/Views/Home/Index.cshtml):

@model A

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.EditorFor(x => x.Name)
    </div>
    @Html.EditorFor(x => x.Bs)
    <button type="submit">OK</button>
}

将为 Bs 集合的每个元素呈现相应的编辑器模板(~/Views/Home/ EditorTemplates/B.cshtml):

@model B
<div>
    @Html.LabelFor(x => x.Val)
    @Html.EditorFor(x => x.Val)
</div>

Your model has circular references in your view models. That's not a supported scenario by the default model binder. I would recommend you to always use editor templates in your views. Example:

Model:

public  class A
{
    public int Name { get; set; }
    public virtual ICollection<B> Bs { get; set; }
}

public class B
{
    public int Val { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new A
        {
            Name = 123,
            Bs = new[]
            {
                new B { Val = 1 },
                new B { Val = 2 },
                new B { Val = 3 },
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(A a)
    {
        // The Bs collection will be properly bound here
        return View(a);
    }
}

View (~/Views/Home/Index.cshtml):

@model A

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.EditorFor(x => x.Name)
    </div>
    @Html.EditorFor(x => x.Bs)
    <button type="submit">OK</button>
}

Corresponding editor template which will be rendered for each element of the Bs collection (~/Views/Home/EditorTemplates/B.cshtml):

@model B
<div>
    @Html.LabelFor(x => x.Val)
    @Html.EditorFor(x => x.Val)
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文