C# c重写字符串描述 { 获取{返回this.PostDescription;} } } 公共类LiveStream:AbstractThing { 公共字符串用户名{get;set;} 公共字符串状态{get;set;} 公共重写字符串用户名 { 获取{返回this.UserName;} } 公共重写字符串描述 { 获取{返回此.Status;} } } 公共类替换ThingsViewModel { 公共ICollection AbstractThings{get;private set;} 公共替换ThingsViewModel(ICollection lps、ICollection lss) { this.AbstractThings=新列表(); /*这是不正确的…这里是您将“两个接两个”按所需顺序向this.AbstractThings添加项的位置。关键是让viewmodel公开一个集合*/ 如果(null!=lps) { foreach(lps中的抽象事物) { this.AbstractThings.Add(at); } } 如果(null!=lss) { foreach(lss中的抽象事物) { this.AbstractThings.Add(at); } } } }
现在,在单个集合上循环C# c重写字符串描述 { 获取{返回this.PostDescription;} } } 公共类LiveStream:AbstractThing { 公共字符串用户名{get;set;} 公共字符串状态{get;set;} 公共重写字符串用户名 { 获取{返回this.UserName;} } 公共重写字符串描述 { 获取{返回此.Status;} } } 公共类替换ThingsViewModel { 公共ICollection AbstractThings{get;private set;} 公共替换ThingsViewModel(ICollection lps、ICollection lss) { this.AbstractThings=新列表(); /*这是不正确的…这里是您将“两个接两个”按所需顺序向this.AbstractThings添加项的位置。关键是让viewmodel公开一个集合*/ 如果(null!=lps) { foreach(lps中的抽象事物) { this.AbstractThings.Add(at); } } 如果(null!=lss) { foreach(lss中的抽象事物) { this.AbstractThings.Add(at); } } } },c#,html,razor,C#,Html,Razor,现在,在单个集合上循环 @foreach (var post in Model.AbstractThings) { <div id="divLivePost" class="well-liveposts well-sm"> <div class="row"> <div class="col-md-2"> <div class="ui-icon-image">
@foreach (var post in Model.AbstractThings)
{
<div id="divLivePost" class="well-liveposts well-sm">
<div class="row">
<div class="col-md-2">
<div class="ui-icon-image">
<img src="~/Images/placeholder.jpg" width="100%" />
</div>
</div>
<div class="col-md-10">
<div class="text-left row">
<div class="col-md-12">
@post.TheUserName
</div>
<div class="col-md-12">
@post.TheDescription
</div>
</div>
</div>
</div>
</div>
}
@foreach(Model.AbstractThings中的var post)
{
@post.TheUserName
@后描述
}
下面是一个“一个接着另一个”的实现…只要LivePost计数大于LiveStream。并且没有错误检查。但是给你一个想法
public class AlternatingThingsViewModel
{
public ICollection<AbstractThing> AbstractThings { get; private set; }
public AlternatingThingsViewModel(ICollection<LivePost> lps, ICollection<LiveStream> lss)
{
this.AbstractThings = new List<AbstractThing>();
/* this is a "one by one" with no error checking if the counts are different */
IEnumerator<AbstractThing> livePostEnum = null == lps ? null : lps.GetEnumerator();
IEnumerator<AbstractThing> liveStreamEnum = null == lss ? null : lss.GetEnumerator();
if (null != liveStreamEnum)
{
liveStreamEnum.MoveNext();
}
if (null != livePostEnum)
{
while (livePostEnum.MoveNext() == true)
{
AbstractThing lpCurrent = livePostEnum.Current;
AbstractThing lsCurrent = null == liveStreamEnum ? null : liveStreamEnum.Current;
if (null != liveStreamEnum)
{
liveStreamEnum.MoveNext();
}
this.AbstractThings.Add(lpCurrent);
if (null != lsCurrent)
{
this.AbstractThings.Add(lsCurrent);
}
}
}
}
}
公共类替换ThingsViewModel
{
公共ICollection AbstractThings{get;private set;}
公共替换ThingsViewModel(ICollection lps、ICollection lss)
{
this.AbstractThings=新列表();
/*这是一个“逐个”的过程,如果计数不同,则不会进行错误检查*/
IEnumerator livePostEnum=null==lps?null:lps.GetEnumerator();
IEnumerator liveStreamEnum=null==lss?null:lss.GetEnumerator();
if(null!=livestreamnum)
{
livestreamnum.MoveNext();
}
if(null!=livePostEnum)
{
while(livePostEnum.MoveNext()==true)
{
AbstractThing lpCurrent=livePostEnum.Current;
AbstractThing lsCurrent=null==livestreamnum?null:livestreamnum.Current;
if(null!=livestreamnum)
{
livestreamnum.MoveNext();
}
this.AbstractThings.Add(lpCurrent);
if(null!=lsCurrent)
{
this.AbstractThings.Add(lsCurrent);
}
}
}
}
}
还有一些呼叫代码
static void Main(string[] args)
{
try
{
ICollection<LivePost> lps = new List<LivePost>();
lps.Add(new LivePost() { UserName = "LivePostUserName1", PostDescription = "LivePostDescription1" });
lps.Add(new LivePost() { UserName = "LivePostUserName2", PostDescription = "LivePostDescription2" });
lps.Add(new LivePost() { UserName = "LivePostUserName3", PostDescription = "LivePostDescription3" });
lps.Add(new LivePost() { UserName = "LivePostUserName4", PostDescription = "LivePostDescription4" });
ICollection<LiveStream> lss = new List<LiveStream>();
lss.Add(new LiveStream() { UserName = "LiveStreamUserName1", Status = "LiveStreamStatus1" });
lss.Add(new LiveStream() { UserName = "LiveStreamUserName2", Status = "LiveStreamStatus2" });
//lss.Add(new LiveStream() { UserName = "LiveStreamUserName3", Status = "LiveStreamStatus3" });
AlternatingThingsViewModel atmv = new AlternatingThingsViewModel(lps, lss);
int modCheckCount = 0;
foreach (AbstractThing at in atmv.AbstractThings)
{
Console.WriteLine("{0}, {1}", at.TheUserName, at.TheDescription);
if (++modCheckCount % 2 == 0)
{
Console.WriteLine("");
}
}
static void Main(字符串[]args)
{
尝试
{
ICollection lps=新列表();
添加(新的LivePost(){UserName=“livepostername1”,postsdescription=“livepostsdescription1”});
添加(新的LivePost(){UserName=“livepostername2”,postsdescription=“livepostsdescription2”});
添加(新的LivePost(){UserName=“livepostername3”,postsdescription=“livepostsdescription3”});
添加(新的LivePost(){UserName=“livepostername4”,postsdescription=“livepostsdescription4”});
ICollection lss=新列表();
添加(新的LiveStream(){UserName=“LiveStreamUserName1”,Status=“LiveStreamStatus1”});
添加(新的LiveStream(){UserName=“LiveStreamUserName2”,Status=“LiveStreamStatus2”});
//添加(新的LiveStream(){UserName=“LiveStreamUserName3”,Status=“LiveStreamStatus3”});
AlternatingThingsViewModel atmv=新的AlternatingThingsViewModel(lps、lss);
int modCheckCount=0;
foreach(atmv中的AbstractThing.AbstractThings)
{
WriteLine(“{0},{1}”,at.TheUserName,at.TheDescription);
如果(++modCheckCount%2==0)
{
控制台。写线(“”);
}
}
好吧,那么-是的-在控制器中进行操作显然是有意义的。但是,再说一次,Razor只是(大部分)C#所以无论你在一个控制器中做什么,你都可以在另一个控制器中做:
@{
var streamIndex = 0;
var postIndex = 0;
}
@while (streamIndex < Model.LiveStreams.Count() || postIndex < Model.LivePosts.Count()) {
@foreach (var stream in Model.LiveStreams.Skip(streamIndex).Take(2)) {
// html code
streamIndex++;
}
@foreach (var post in Model.LivePosts.Skip(postIndex).Take(2)) {
// html code
postIndex++;
}
}
@{
var-streamIndex=0;
var postIndex=0;
}
@而(streamIndex
好吧,那么-是的-在控制器中进行操作显然是有意义的。但是,再说一次,Razor只是(大部分)C#所以无论你在一个控制器中做什么,你都可以在另一个控制器中做:
@{
var streamIndex = 0;
var postIndex = 0;
}
@while (streamIndex < Model.LiveStreams.Count() || postIndex < Model.LivePosts.Count()) {
@foreach (var stream in Model.LiveStreams.Skip(streamIndex).Take(2)) {
// html code
streamIndex++;
}
@foreach (var post in Model.LivePosts.Skip(postIndex).Take(2)) {
// html code
postIndex++;
}
}
@{
var-streamIndex=0;
var postIndex=0;
}
@而(streamIndex
waaa?我确实有一个ViewModel。我会更新以显示这一点。你能详细说明一下吗?很好,我很感激。我已经编辑了我的问题以添加我正在使用的当前ViewModel。ViewModel在绑定之前需要“完美”。这就是ViewModel的要点…“按摩数据”以便可以显示。你不想这样做吗“跳跃”。你需要在AlternatingThingsView模型中封装“两个接两个”的逻辑,并处理“当我有50个LivePost和3个LiveStreams时会发生什么”之类的事情。啊,亲爱的。我会给你答案
@foreach (var post in Model.AbstractThings)
{
<div id="divLivePost" class="well-liveposts well-sm">
<div class="row">
<div class="col-md-2">
<div class="ui-icon-image">
<img src="~/Images/placeholder.jpg" width="100%" />
</div>
</div>
<div class="col-md-10">
<div class="text-left row">
<div class="col-md-12">
@post.TheUserName
</div>
<div class="col-md-12">
@post.TheDescription
</div>
</div>
</div>
</div>
</div>
}
public class AlternatingThingsViewModel
{
public ICollection<AbstractThing> AbstractThings { get; private set; }
public AlternatingThingsViewModel(ICollection<LivePost> lps, ICollection<LiveStream> lss)
{
this.AbstractThings = new List<AbstractThing>();
/* this is a "one by one" with no error checking if the counts are different */
IEnumerator<AbstractThing> livePostEnum = null == lps ? null : lps.GetEnumerator();
IEnumerator<AbstractThing> liveStreamEnum = null == lss ? null : lss.GetEnumerator();
if (null != liveStreamEnum)
{
liveStreamEnum.MoveNext();
}
if (null != livePostEnum)
{
while (livePostEnum.MoveNext() == true)
{
AbstractThing lpCurrent = livePostEnum.Current;
AbstractThing lsCurrent = null == liveStreamEnum ? null : liveStreamEnum.Current;
if (null != liveStreamEnum)
{
liveStreamEnum.MoveNext();
}
this.AbstractThings.Add(lpCurrent);
if (null != lsCurrent)
{
this.AbstractThings.Add(lsCurrent);
}
}
}
}
}
static void Main(string[] args)
{
try
{
ICollection<LivePost> lps = new List<LivePost>();
lps.Add(new LivePost() { UserName = "LivePostUserName1", PostDescription = "LivePostDescription1" });
lps.Add(new LivePost() { UserName = "LivePostUserName2", PostDescription = "LivePostDescription2" });
lps.Add(new LivePost() { UserName = "LivePostUserName3", PostDescription = "LivePostDescription3" });
lps.Add(new LivePost() { UserName = "LivePostUserName4", PostDescription = "LivePostDescription4" });
ICollection<LiveStream> lss = new List<LiveStream>();
lss.Add(new LiveStream() { UserName = "LiveStreamUserName1", Status = "LiveStreamStatus1" });
lss.Add(new LiveStream() { UserName = "LiveStreamUserName2", Status = "LiveStreamStatus2" });
//lss.Add(new LiveStream() { UserName = "LiveStreamUserName3", Status = "LiveStreamStatus3" });
AlternatingThingsViewModel atmv = new AlternatingThingsViewModel(lps, lss);
int modCheckCount = 0;
foreach (AbstractThing at in atmv.AbstractThings)
{
Console.WriteLine("{0}, {1}", at.TheUserName, at.TheDescription);
if (++modCheckCount % 2 == 0)
{
Console.WriteLine("");
}
}
@{
var streamIndex = 0;
var postIndex = 0;
}
@while (streamIndex < Model.LiveStreams.Count() || postIndex < Model.LivePosts.Count()) {
@foreach (var stream in Model.LiveStreams.Skip(streamIndex).Take(2)) {
// html code
streamIndex++;
}
@foreach (var post in Model.LivePosts.Skip(postIndex).Take(2)) {
// html code
postIndex++;
}
}