How to generate two columns in loop on asp net view - asp.net-mvc

I would like to get two columns side by side in one row. Data in these columns will be different.
Like this:
<div class="row">
<div class="col-md-6">
Data1
</div>
<div class="col-md-6">
Data2
</div>
</div>
But.. my code generate a structure:
<div class="row">
<div class="col-md-6 text-center">
</div>
<div class="col-md-6 text-center">
<h2>Title: </h2>
<p>Position: Name Surname</p>
</div>
<div class="row">
<div class="col-md-6 text-center">
<h2>Title: </h2>
<p>Position: Name Surname</p>
</div>
<div class="col-md-6 text-center">
</div>
</div>
This is my code in PartialView (_Pozycjee.cshtml):
#model IEnumerable<dynamic>
#if (Model.Any() == true)
{
foreach (dynamic item in Model)
{
#:<div class="row">
<div class="col-md-6 text-center">
#if (item.Mecz.GospodarzKlubId == item.Zawodnik.KlubId)
{
<h2>Skład gospodarzy:</h2>
<p>#item.Nazwa: #item.Zawodnik.Imie #item.Zawodnik.Nazwisko</p>
}
</div>
<div class="col-md-6 text-center">
#if (item.Mecz.GoscKlubId == item.Zawodnik.KlubId)
{
<h2>Skład gości:</h2>
<p>#item.Nazwa: #item.Zawodnik.Imie #item.Zawodnik.Nazwisko</p>
}
</div>
}
#: </div>
}
else
{
<p>Brak pozycji</p>
}
This generate a "empty" div and and spoils everything.
Look at this screen: https://photoland.io/i/k4Pzu
Please for Help. Greetings

Try this:
#model IEnumerable<dynamic>
#if (Model.Any() == true)
{
<div class="row">
#foreach (dynamic item in Model)
{
if (item.Mecz.GospodarzKlubId == item.Zawodnik.KlubId)
{
<div class="col-md-6 text-center">
<h2>Skład gospodarzy:</h2>
<p>#item.Nazwa: #item.Zawodnik.Imie #item.Zawodnik.Nazwisko</p>
</div>
}
if(item.Mecz.GoscKlubId == item.Zawodnik.KlubId)
{
<div class="col-md-6 text-center">
<h2>Skład gości:</h2>
<p>#item.Nazwa: #item.Zawodnik.Imie #item.Zawodnik.Nazwisko</p>
</div>
}
}
</div>
}
else
{
<p>Brak pozycji</p>
}

Related

How to add div tag after fixed number of iteration in mvc

I am using Umbraco (mvc) with bootstrap.My template use partial view to load name and job title of staff in template. How I can add div (new row) after 4 iteration.
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
var selection = Model.Content.Site().FirstChild("aboutUs").Children("staff")
.Where(x => x.IsVisible());
}
#foreach(var item in selection){
<div class="row team">
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#item.GetPropertyValue("fullName")</a> <small>#item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
</div>
}
Difficulty:
I want to add
<div class="row team">
</div>
after every 4th
<div class="col-sm-3">
</div>
A more easier solution would be the use of the Umbraco helper method InGroupsOf(columns).
#foreach(var group in selection.InGroupsOf(4))
{
<div class="row team">
#foreach(var item in group)
{
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#item.GetPropertyValue("fullName")</a> <small>#item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
}
</div>
}
Try:
#foreach(var obj in selection.Select((item, index) => new {item, index}))
{
if(obj.index==0||obj.index%4==0){
#Html.Raw("<div class='row team'>")
}
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#obj.item.GetPropertyValue("fullName")</a> <small>
#obj.item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
#if((obj.index+1)%4==0||obj.index+1==selection.Count()){
#Html.Raw("</div>")
}
}
- It will works, Use for loop instead of foreach loop and then just add logic
#for (int i = 1; i < selection.Count; i++)
{
var item = selection[i];
<div class="row team">
<div class="col-sm-3">
<div class="thumbnail-style">
#if (i % 5 == 0)
{
<div class="col-sm-3">
<h4 >D #item</h4> <!--your actual html code-->
</div>
}
else
{
<h4 style="color:red">D #item</h4> <!--your actual html code-->
}
</div>
</div>
</div>
}

Grouping/GROUP BY With LINQ in MVC

I am new in MVC. In this page; I list the projects belonging to the "unit name" by loop. And what I want to do: I want to group projects belonging to the same "unit name". Each project has a "unit name" to which it belongs. There can be more than one project belonging to one unit. So I want to group it.
My cshtml code is as follows:
#if (Model.Projects.Any())
{
foreach (var item in Model.Projects.ToList())
{
<div class="ProjectPartialBody" data-id="#item.ID">
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption">
<span><i class="glyphicon glyphicon-asterisk"></i> #item.tbl_Unit.Name</span>
</div>
<div class="tools">
</div>
<div class="actions">
Details
</div>
</div>
<div class="portlet-body">
<span><i class="fa fa-file"></i> #item.Name</span>
<span><i class="fa fa-calendar"> Contract Start and End Dates: #(item.ContractStartDate != null ? item.ContractStartDate.Value.ToString("dd.MM.yyyy") : "-") / #(item.ContractEndDate != null ? item.ContractEndDate.Value.ToString("dd.MM.yyyy") : "-")</i></span>
<div class="well">
<div class="form-group">
<label>Cash Completion Rate: %#item.CashCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: #(item.CashCompletionRate)%;"></div>
</div>
</div>
</div>
<div class="well">
<div class="form-group">
<label>Physical Completion Rate: %#item.PhysicalCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: #(item.PhysicalCompletionRate)%;"></div>
</div>
</div>
</div>
</div>
</div>
</div>
}
}
And the view of the page is as follows (my view is in Turkish language but you'll understand what I am talking about):
My Page View
Can you please help me? And if there is any other code block you want to insert, please tell me.
You first need to group by in your for statement:
foreach(var unitGroup in Model.Projects.GroupBy(g => g.tbl_Unit.Name))
{
//...your MVC markup here
}
Now you actualy need to change the markup as you would need to put in another foreach loop that would generate markup for projects.
So something simillar to this.
<div class="ProjectPartialBody" data-id="#item.ID">
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption">
<span><i class="glyphicon glyphicon-asterisk"></i> #item.tbl_Unit.Name</span>
</div>
<div class="tools">
</div>
<div class="actions">
Details
</div>
</div>
#foreach(var project in unitGroup.ToList())
{
<div class="portlet-body">
<span><i class="fa fa-file"></i> #project.Name</span>
<span><i class="fa fa-calendar"> Contract Start and End Dates: #(project.ContractStartDate != null ? project.ContractStartDate.Value.ToString("dd.MM.yyyy") : "-") / #(project.ContractEndDate != null ? project.ContractEndDate.Value.ToString("dd.MM.yyyy") : "-")</i></span>
<div class="well">
<div class="form-group">
<label>Cash Completion Rate: %#project.CashCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: #(project.CashCompletionRate)%;"></div>
</div>
</div>
</div>
<div class="well">
<div class="form-group">
<label>Physical Completion Rate: %#project.PhysicalCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: #(project.PhysicalCompletionRate)%;"></div>
</div>
</div>
</div>
</div>
}
</div>
</div>
There should be some errors here as I am not very familliar with your model but this should be the general idea behind it.

How to show your hidden partial view programmatically in MVC

I have a partial view _ABC.cshtml that is hidden initially.
<div id="overview" style="display:none">
<h1> Overview</h1>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 leftlist">
</div>
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 overview">
<h3 class="isppagetitle">#Resources.Overview</h3>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 content">
<div class="row">
......
But now from other view I want it to be rendered .
public ActionResult Index()
{
return PartialView("_AMgmtPartial");
}
I did in _AMgmtPartial..
<div class="account-root">
#{
Html.RenderPartial("_CustList");
Html.RenderPartial("_ABC", new { style = "display:block" });
}
<input type="hidden" id="accountmgmturl" value='#Url.Action("", "AccountMgmt")' />
...............
...............
But this style="display:block;" did not work. What did I do wrong ?
Html.RenderPartial doesn't have a parameter that accepts HtmlAttributes. This is confirmed by looking at the documentation for it.
The parameter that you have populated would be accessible from within the Controller though:
public ActionResult Index(string style)
{
// style will equal "display:block"
return PartialView("_AMgmtPartial");
}
You could take this value and pass it to your partial view via the view bag or a view model.
public ActionResult Index(string style = "display: none")
{
// style will equal "display:block"
ViewBag.PartialStyle = style;
return PartialView("_AMgmtPartial");
}
Then this makes it accessible from within your view:
<div id="overview" style="#ViewBag.PartialStyle">
<h1> Overview</h1>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 leftlist">
</div>
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 overview">
<h3 class="isppagetitle">#Resources.Overview</h3>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 content">
<div class="row">
......
To do this though would be a dirty hack. As #Stephen mentioned a more preferable solution would be to wrap your partial view render statement in a <div> and on that you should set your initial visibility.
<div style="display: block">
#Html.Partial("_ABC");
</div>

Render Partial View in Layout MVC 4 with Caching and Data

I'm new to MVC.NET and I'm working with MVC4 .
In a project I should return System Information like Post count, User counts and etc. In My _Layout I have defined a Sidebar which will show these data.
here is my sidebar div
<div class="sideElement">
<div class="header">
مشخصات سیستم
</div>
<div class="body">
#{Html.Action("GatherSystemInfo","Home").ToString();}
</div>
</div>
My GatherSystemInfo Action is Like This :
Of Course I have used static data For testing
[OutputCache(Duration=900, VaryByParam = "none")]
[ChildActionOnly]
public ActionResult GatherSystemInfo(SystemInformation model = null)
{
model.QuestionCount = 10930;
model.ToturialCount = 10353;
model.UserCount = 120123;
model.ProjectsNumber = 10231;
model.DateTime = DateTime.Now.ToLongTimeString();
model.OnlineUsers = 28;
return PartialView("_SystemInfo", model);
}
And My Partial View Is like this:
#model Persiangeeks.Models.SystemInformation
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد سوالات ثبت شده : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.QuestionCount)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد آموزشهای موجود : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.ToturialCount)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد پروژه های موجود : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.ProjectsNumber)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد کاربران ثبت شده : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.UserCount) نفر</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد کاربران آنلاین : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.OnlineUsers) نفر</div>
<div class="clear" ></div>
</div>
But When I run my project I have Nothing to show in my sidebar div. here is the Image
Thank you for your help :)
Html.Action() – Outputs string
Html.RenderAction() – Renders directly to response\
Solution : use RenderAction() in place of Action()

(MVC 3 Razor) - Easier way to loop through 3 column div

At the moment I'm using the following code in my CSHTML:
#{int i = 0;}
#foreach (var item in Model.Traders)
{
if ((i++ % 3) == 0) {
if (i != 1) {
#:</div>
}
#:<div class="row">
}
#:<div class="four column"><div class="panel new"><h3 class="dotted"><strong>#item.Title</strong></h3><p>#item.Description</p><code><div class="panel pick"></code></div></div>
}
#if (i != 0) {
#:</div>
}
This outputs the following HTML:
<div class="row">
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>Title</strong></h3><p>Description</p><code>code</code></div></div>
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>Title</strong></h3><p>Description</p><code>code</code></div></div>
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>Title</strong></h3><p>Description</p><code>code</code></div></div>
</div>
<div class="row">
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>Bobby</strong></h3><p>Bobby bobby bobby</p><code><div class="panel pick"></code></div></div>
<!-- Add missing divs if there's less than 3 (there always needs to be 3 divs inside a div row). In this case it's 2 that are missing -->
<div class="four column"></div> <!-- my code does not render these -->
<div class="four column"></div> <!-- my code does not render these -->
</div>
My question is whether there's an easier way to achieve what I'm doing within my view and to ensure that it adds the missing divs if there's less than 3 in a row.
Group traders in batch of 3 items, Try this:
#{
var g = Model.Traders.GroupBy(r => Model.Traders.IndexOf(r) / 3).ToList();
}
#foreach (var parentItem in g)
{
<div class="row">
#foreach (var item in parentItem)
{
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>#item.Title</strong></h3><p>#item.Description</p><code><div class="panel pick"></code></div></div>
}
#for (int i = parentItem.Count(); i < 3; i++)
{
<div class="four column"></div>
}
</div>
}
Regards.

Resources