[英]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中编辑变量的值,例如
Comment
和Value
,但要使用使用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.