如何通过&在索引和编辑视图中合并 MVC3 中的两个模型(强绑定)?

发布于 2024-12-11 04:19:56 字数 1733 浏览 0 评论 0原文

模型/数据库实体关系(基线占位符文学内容):

public class TitlePercent
{
   public int TitleName; 
   public decimal Percentage;      
}

public class TitleMonthly
{
   public string Month;
   public int Val;
}

基数 => 1 TitlePercent 至 * TitleMonthly

Illustrator 视图(建议):

_____________________________
Name  | % ||  Jan | Feb |
_____________________________
Title1 2%     23    343
Title2 3%     343   3434
_____________________________

控制器(建议):

    // Need view and edit   
    public ViewResult ViewTitlePercentAndTitleMontly()
    {
        return View(db.<NotSureWhatWouldGoHere>.ToList());
    }

    public ActionResult EditTitlePercentAndTitleMontly(int id)
    {
        return View(db.<NotSureWhatWouldGoHere>.Find(id));
    }

Razor 视图(建议 psuedo): ...查看和编辑 @model MVC3.Models.TitlePercent @model2 MVC3.Models.TitleMonthly

1. For Index View to show the grid not sure how to mash this up (like the grid)
   - Perhaps build this in the controller but not sure how to build and pass that to the view as
     I have had not dealt with multiple models as relationships.
2. For Edit View need to figure out how to bind to a model similar to:
    @Html.EditorFor(model => model.TitleName)
    @Html.EditorFor(model => model.Percentage)

    foreach TitleMonthly in TitlePercentag
    {
       @* Iterate and bind somehow *@
       @Html.EditorFor(model2 => model2.Month)
       @Html.EditorFor(model2 => model2.Val)
    }
    ???

抱歉缺乏细节和伪代码,但我只是不必处理多个模型,特别是如果它们彼此相关/依赖,并且可以通过连接创建网格视图类/表....那么如果您想编辑一行(两个类的组合,那么如何将所有关系绑定到基于这两个类模型的文本框)?

再次,我将对此进行嘲笑,但任何信息和示例将不胜感激。

Model/Db Entity Relationship (baseline placeholder literary content):

public class TitlePercent
{
   public int TitleName; 
   public decimal Percentage;      
}

public class TitleMonthly
{
   public string Month;
   public int Val;
}

Cardinality => 1 TitlePercent to * TitleMonthly

Illustrated View (proposed):

_____________________________
Name  | % ||  Jan | Feb |
_____________________________
Title1 2%     23    343
Title2 3%     343   3434
_____________________________

Controller (proposed):

    // Need view and edit   
    public ViewResult ViewTitlePercentAndTitleMontly()
    {
        return View(db.<NotSureWhatWouldGoHere>.ToList());
    }

    public ActionResult EditTitlePercentAndTitleMontly(int id)
    {
        return View(db.<NotSureWhatWouldGoHere>.Find(id));
    }

Razor View (proposed psuedo):
...View and Edit
@model MVC3.Models.TitlePercent
@model2 MVC3.Models.TitleMonthly

1. For Index View to show the grid not sure how to mash this up (like the grid)
   - Perhaps build this in the controller but not sure how to build and pass that to the view as
     I have had not dealt with multiple models as relationships.
2. For Edit View need to figure out how to bind to a model similar to:
    @Html.EditorFor(model => model.TitleName)
    @Html.EditorFor(model => model.Percentage)

    foreach TitleMonthly in TitlePercentag
    {
       @* Iterate and bind somehow *@
       @Html.EditorFor(model2 => model2.Month)
       @Html.EditorFor(model2 => model2.Val)
    }
    ???

Sorry for the lack of details and pseudo code but I just haven't had to deal with multiple models especially if they are related/dependencies of each other and and in which the grid view can be created by joined classes/tables....then if you want to edit a row (combo of both classes then how can you bind all relationships to text boxes based on those two class models)?

Again, I'll submit to ridicule on this one but any information and examples would be greatly appreciated.

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

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

发布评论

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

评论(1

心欲静而疯不止 2024-12-18 04:19:56

通常您使用名为 ViewModel 的东西,它是为视图设计的非常具体的模型。您可以使用数据模型中的信息填充该模型,然后将其传递给视图。它也不必具有 1:1 的结构,这可以让您获得更多或更少的信息,具体取决于您的需求。

对于您的编辑器模型,您可以创建一个类似于以下内容的 ViewModel:

public class TitleEditingViewModel { 
    public TitlePercent TitlePercent {get;set;}
    public ICollection<TitleMonthly> MonthlyTitles {get;set;}
}

然后,您可以在控制器中以您想要的方式填充该模型,然后将模型传递到视图中。

@Html.EditorFor(model => model.TitlePercent.TitleName)
@Html.EditorFor(model => model.TitlePercent.Percentage)

@foreach titleMonthly in Model.MonthlyTitles {
   @Html.EditorFor(model => titleMonthly.Month)
   @Html.EditorFor(model => titleMonthly.Val)
}

Usually you use something called a ViewModel which is a very specific model designed for a view. You would populate that model with information from your data model and then pass that to the View. it doesn't have to have a 1:1 structure either which allows you to have a lot more information or a lot less, depending on your needs.

For your editor model you could create a ViewModel similar to:

public class TitleEditingViewModel { 
    public TitlePercent TitlePercent {get;set;}
    public ICollection<TitleMonthly> MonthlyTitles {get;set;}
}

You would then populate that model however you want in the controller and then pass the model into the view.

@Html.EditorFor(model => model.TitlePercent.TitleName)
@Html.EditorFor(model => model.TitlePercent.Percentage)

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