简体   繁体   English

从其他模型的剃刀视图编辑参考模型的值

[英]Editing a referenced model's values from a different model's razor view

This is my first time using MVC in a business capacity, so please forgive me if this is an easily answered question. 这是我第一次以商业身份使用MVC,因此如果这是一个容易回答的问题,请原谅我。

My issue is that I have a razor view for editing a model, we will call it MainModel . 我的问题是我有一个用于编辑模型的剃刀视图,我们将其MainModel Here is a reduced version of MainModel: 这是MainModel的简化版本:

using System;
using System.Collections.Generic;

namespace MyProject.Models
{
public partial class MainModel
{
    public MainModel()
    {
        SecondModel = new HashSet<SecondModel>();
    }

    public int MainModelId { get; set; }
    public DateTime? DueDate { get; set; }
    public int? CreatedByUserId { get; set; }
    public DateTime? CreatedDateTime { get; set; }
    public bool IsActive { get; set; }

    public virtual ICollection<SecondModel> SecondModel { get; set; }
    public virtual Users CreatedByUser { get; set; }
}
}

MainModel has a reference to another Model, SecondModel (original names, I know). MainModel引用了另一个模型SecondModel (原始名称,我知道)。 This reference is a collection so that it can be looped through with a foreach within the razor view. 该引用是一个集合,因此可以在剃刀视图中与foreach一起循环使用。 Here is SecondModel: 这是SecondModel:

namespace MyProject.Models
{
public partial class SecondModel
{
    public int SecondModelId { get; set; }
    public string Comment { get; set; }
    public double? Value { get; set; }
    public int? ThirdModelId { get; set; }
    public bool IsActive { get; set; }

    public virtual ThirdModel ThirdModelVariable { get; set; }
}
}

So my issue is that I need to be able to edit the values of variables such as Comment and Value within SecondModel, but from the razor edit page that is using the model from MainModel. 因此,我的问题是我需要能够在SecondModel中编辑变量的值,例如CommentValue ,但要使用使用MainModel中的模型的剃刀编辑页面。

I've tried a few different variation of this, with no luck. 我试过了几种不同的变体,没有运气。

<input asp-for="SecondModel.Comment" class="form-control" />

Usually trying something like this presents the following error: 通常尝试这样的操作会出现以下错误:

ICollection<SecondModel> does not contain a definition for 'Comment' and no extension method 'Comment' accepting a first argument of type 'ICollection<SecondModel>' could be found

You may have noticed that there is also a reference to a ThirdModel within SecondModel. 您可能已经注意到, ThirdModel也有对ThirdModel的引用。 I also need to be able to reference some variables from that model within this view, but I assume the solution for one will be the solution for both in this case. 我还需要能够在此视图中引用该模型中的某些变量,但是在这种情况下,我认为一个解决方案就是两个解决方案。 Hopefully I explained that well enough, please let me know if you need to see anymore code to reference or if I wasn't clear enough. 希望我已经解释得足够好了,如果您需要查看更多代码以供参考或不清楚的话,请告诉我。

Thanks for your time! 谢谢你的时间!

When you specify to what input should be bound, you must specify valid C# accessor (behind the scene this is interpreted as lambda expression which represents access to specific property/field). 当您指定绑定到什么输入时,必须指定有效的C#访问器(在幕后将其解释为代表对特定属性/字段的访问的lambda表达式)。 Error message is telling you that ICollection<SecondModel> does not have property Comment , which is correct since SecondModel property is of type ICollection<SecondModel> and not SecondModel . 错误消息告诉您ICollection<SecondModel>没有属性Comment ,这是正确的,因为SecondModel属性的类型为ICollection<SecondModel>而不是SecondModel

To fix your error, you have to retrieve specific element from collection first. 要纠正错误,您必须首先从集合中检索特定元素。 So you can either loop the collection and generate input for every element or access specific element if you know it is there. 因此,您可以循环收集并为每个元素生成输入,或者在知道特定元素的情况下访问特定元素。 For accessing element you have a problem with your collection type since you cannot use indexer. 对于访问元素,您无法使用索引器,因此您的集合类型存在问题。 You should change it to IList<SecondModel> . 您应该将其更改为IList<SecondModel> Then you can for example access write input for comment field for first element in collection like this: 然后,例如,您可以像这样访问集合中第一个元素的注释字段的写输入:

<input asp-for="SecondModel[0].Comment" class="form-control" />

And for accessing ThirdModel : 并访问ThirdModel

<input asp-for="SecondModel[0].ThirdModel.SomeProperty" class="form-control" />

I suggest reading Model binding to a list . 我建议阅读将Model绑定到列表 It is quite old post which does not use tag helpers, but behavior is the same. 这是一个很老的帖子,它不使用标签助手,但是行为是相同的。

ICollection<SecondModel> does not contain a definition for 'Comment'` ICollection<SecondModel>不包含“注释”的定义。

The error is clear. 错误很明显。 You're looking for Comment property in a collection. 您正在寻找集合中的Comment属性。 It should be 它应该是

@for (int i=0; i< Model.SecondModel.Count(); i++)
{
    <input asp-for="SecondModel[i].Comment" class="form-control" />
    // other properties
}

or use the HTML Helpers : 或使用HTML Helpers

@for (int i=0; i< Model.SecondModel.Count(); i++)
{
    @Html.TextBoxFor(m => m.SecondModel[i].Comment, new { @class = "form-control" })
     // other properties

    @Html.TextBoxFor(m => m.SecondModel[i].ThirdModelVariable.Property, new { @class = "form-control" })

}

For the indexers to work, you'd have to change the property from an ICollection<SecondModel> to IList<SecondModel> 为了使索引器正常工作,您必须将属性从ICollection<SecondModel>更改为IList<SecondModel>

To avoid this kind of confusion, whenever you have a IList<T> type property, it's better to name them as plurals. 为避免这种混淆,每当您拥有IList<T>类型的属性时,最好将它们命名为复数形式。 In your case: 在您的情况下:

public virtual IList<SecondModel> SecondModels { get; set; }

Now you'll know you're referring to the SecondModels property rather than keeping it SecondModel and forgetting it's a collection. 现在您将知道引用的是SecondModels属性,而不是保留SecondModel并忘记了它是一个集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC Razor 视图嵌套 foreach 的模型 - MVC Razor view nested foreach's model 在视图的引擎剃刀中传递参数作为模型 - Passing a parameter as model in view's engine razor 将模型提交给与视图模型类型不同的操作 - Submitting a model to action that is different from view model's type 如果将剃刀视图引用到视图模型而不是模型,则jQuery calender无法正常工作 - Jquery calender is not working if the razor view is referenced to a view model instead of a model 从不同窗口的视图模型调用窗口中的函数 - Calling a function in a Window from a different Window's View Model 从ActionFilterAttribute返回带有它的模型的视图 - Returning a view with it's model from an ActionFilterAttribute 是否有可能以编程方式确定ASP.NET MVC的Razor View模型? - Is it possible to programatically determine an ASP.NET MVC's Razor View's Model? ASP.NET MVC剃刀视图,发布到与原始模型绑定不同的模型? - ASP.NET MVC razor view, post to a different model from the original model binding? 如何在剃刀视图中将@model指令更改为其他模型? - how to change @model directive to a different model in razor view? 将模型与视图模型分开有什么意义? (MVVM) - What's the point of separating the Model from the View Model? (MVVM)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM