Get object inside partial View - c#

Friends,
I have a View page called "OtherDatas.cshtml" and i need to render a PartialView inside her. So, to make this, i need to check if the object inside partialView is null or not. If not, the partialView Appears. How can i get the object inside the partialView? Here is my code:
OtherDatas.cshtml:
<div class="table-responsive">
#{
if (//I need to check the object inside ){
Html.Partial("~/Views/Outrosdados/Search.cshtml");
}
else
{
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
</table>
}
}
</div>
PartialView Search.html:
#model TB_DADOS_MANUAIS
#using EixoX.Html;
#using Gestão.Models;
#using System.Globalization;
#{
List<TB_OUTROS_DADOS> od = (List<TB_OUTROS_DADOS>)ViewData["outrosDados"];
Bootstrap3Presenter<TB_OUTROS_DADOS> presenter =
Bootstrap3Presenter<TB_OUTROS_DADOS>.GetInstance("pt-BR");
ViewBag.Title = "Outros Dados";
}
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
#foreach (TB_OUTROS_DADOS dados in od){
<tr>
<td>#dados.DT_MES_UTILIZACAO.ToString(CultureInfo.CreateSpecificCulture("pt-BR"))</td>
<td><span class="money" />#dados.VL_AMORTIZACAO</td>
<td><span class="money" />#dados.VL_RECEBIMENTO_MES</td>
<td><span class="money" />#dados.VL_ATRASO</td>
<td><span class="money" />#dados.VL_DAMP3</td>
</tr>
}
</table>
I need to check inside of the 'foreach' if i have some 'TB_OUTROS_DADOS' inside the 'od' list.
Thanks!

You can't do that in this way.
Pass List<> as model to you parent view or declare this list in same way like in partial view then do check, and if it is good pass it to partial view.
In this way:
#{
List<TB_OUTROS_DADOS> od = (List<TB_OUTROS_DADOS>)ViewData["outrosDados"];
<div class="table-responsive">
#{
if (od.Count > 0){
Html.Partial("~/Views/Outrosdados/Search.cshtml",od);
}
else
{
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
</table>
}
}
</div>
And in PartialView
#model List<TB_OUTROS_DADOS>
#using EixoX.Html;
#using Gestão.Models;
#using System.Globalization;
#{
Bootstrap3Presenter<TB_OUTROS_DADOS> presenter =
Bootstrap3Presenter<TB_OUTROS_DADOS>.GetInstance("pt-BR");
ViewBag.Title = "Outros Dados";
}
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
#foreach (TB_OUTROS_DADOS dados in Model){
<tr>
<td>#dados.DT_MES_UTILIZACAO.ToString(CultureInfo.CreateSpecificCulture("pt-BR"))</td>
<td><span class="money" />#dados.VL_AMORTIZACAO</td>
<td><span class="money" />#dados.VL_RECEBIMENTO_MES</td>
<td><span class="money" />#dados.VL_ATRASO</td>
<td><span class="money" />#dados.VL_DAMP3</td>
</tr>
}
</table>

Related

ASP.NET MVC Display HTML Based on User Role

Following best practices, is there a better way to show/hide controls based on the user's role, or is it perfectly acceptable to use User.IsInRole() inside of a view? An answer on this post says it violates separation of concerns. What are the alternatives?
#if(User.IsInRole("Admin"))
{
#Html.ActionLink("Create", "Create", "Games")
}
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Date</th>
<th scope="col">Home</th>
<th scope="col">Away</th>
<th scope="col">Field</th>
<th scope="col">Result</th>
#if (User.IsInRole("Admin"))
{
<th scope="col"></th>
}
</tr>
</thead>
<tbody>
#foreach (var g in Model)
{
<tr>
<th>#g.GameDate</th>
<th>#g.HomeTeam.TeamName</th>
<th>#g.AwayTeam.TeamName</th>
<th>#g.Ballpark.Name</th>
<th>(#g.HomeTeamRuns-#g.AwayTeamRuns) #g.Result</th>
#if (User.IsInRole("Admin"))
{
<th> #Html.ActionLink("Edit", "Edit", new { id = g.GameId })</th>
}
</tr>
}
</tbody>
</table>
Personally, I would add a bool property to the model IsAdmin and set the value in the controller. That way your View is only working with the Model.
#if (Model.IsAdmin)
{
// render admin elements
}

Get cell DisplayName from strongly-typed-list cell by List only (empty list)

I have a list of "Book" (e.g.) as a model in view
#model List<Book>
I want to create a table that each column get it's header by Book's DisplayName propery:
<tr>
<th class="text-right">
#Html.DisplayNameFor(modelItem => Model.someMetaData.lineNumber)
</th>
<th class="text-right">
#Html.DisplayNameFor(modelItem => Model.someMetaData.FirstName)
</th>
<tr>
You can use DisplayNameFor helper method when your model is of type IEnumerable<T>
#model IEnumerable<Book>
<table class="table table-responsive table-striped">
<tr>
<th>#Html.DisplayNameFor(a=>a.Id)</th>
<th>#Html.DisplayNameFor(a => a.Name)</th>
</tr>
#foreach (var b in Model)
{
<tr>
<td>#b.Id</td>
<td>#b.Name</td>
</tr>
}
</table>
This works only if your view is strongly typed to an IEnumerable<Book>, It will not work for List<Book>

Passing a list from Controller to View in ASP.NET MVC 4

I have an Index action method as follows.I am passing a list of Providers to the View.
public ActionResult Index()
{
Provider providerList = new Provider();
List<Provider> providers = DAL.GetListofProviders.ToList();
return View(providers);
}
In the View,I have the following code to receive the List of Providers.
#model IEnumerable<DEMO_JAN14.Models.Provider>
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<title>LIST OF PROVIDERS</title>
</head>
<body>
<table class="table table-striped table-bordered table-hover">
<tr>
<th>Provider Type</th>
<th>First Name</th>
<th>Last Name</th>
<th>Certification</th>
<th>Specialization</th>
<th>SSN</th>
<th>Facility Name</th>
<th>Contact No</th>
<th>Contact Email</th>
<th></th>
</tr>
<tbody data-bind="foreach: viewmodel">
<tr>
<td class="col-lg-2" data-bind="text: ProviderType"></td>
<td class="col-lg-1" data-bind="text: FirstName"></td>
<td class="col-lg-1" data-bind="text: LastName"></td>
<td class="col-lg-1" data-bind="text: Certification"></>
<td class="col-lg-1" data-bind="text: Specialization"></td>
<td class="col-lg-1" data-bind="text: SSN"></td>
<td class="col-lg-4" data-bind="text: FacilityName"></td>
<td class="col-lg-4" data-bind="text: ContactNumber"></td>
<td class="col-lg-1" data-bind="text: ContactEmail"></td>
<td><a class="btn btn-danger" id="del" onclick = "return confirm('Are you sure, you want to delete');" data-bind="attr: { href: '/Provider/Delete/' + ProviderID }"> Delete </a>
</td>
</tr>
</tbody>
I see the list of Providers in the controller
But I dont see the same list in the view as shown
Am I doing something wrong.Please guide me in the right directions.Thanks.
You can iterate like this (using your code)
#model IEnumerable<DEMO_JAN14.Models.Provider>
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<title>LIST OF PROVIDERS</title>
</head>
<body>
<table class="table table-striped table-bordered table-hover">
<tr>
<th>#Html.DisplayNameFor(m => m.ProviderType)</th>
<th>#Html.DisplayNameFor(m => m.FirstName)</th>
...
</tr>
#foreach (var item in Model)
<tr>
<td class="col-lg-2">#Html.DisplayFor(modelItem => item.ProviderType)</td>
<td class="col-lg-1">#Html.DisplayFor(modelItem => item.FirstName)</td>
...
</tr>
</table>
</body>
To check the count of the providers list inside the view use the following property #Model
In your view you can iterate like this on the items, and for example am displaying FirstName of each Provider in the List:
#foreach(var item in Model)
{
<h1>#item.FirstName </h1>
}
Using your html:
<tbody data-bind="foreach: viewmodel">
#foreach(var item in Model)
{
<tr>
<td class="col-lg-2">#item.ProviderType</td>
<td class="col-lg-1">#item.FirstName</td>
........................................
........................................
// and so on other properties
<td><a class="btn btn-danger" id="del" onclick = "return confirm('Are you sure, you want to delete');" href="#Url.Action("Delete","Provider")+ item.ProviderID }"> Delete </a>
</td>
</tr>
you can't access the razor Model object via knockout
you need to receive your data as json result
please refer to this article
http://www.codeproject.com/Articles/424642/Customer-KnockoutJS-and-MVC-demo-using-JSON

Razor get value of input HTML bounded by foreach

I have one following table html, the data of rows in the table is looped by foreach.
Can I use Razor to get the value of each row into a List or an array in C#?
My C# code Razor (I tried this so far)
#{
var l = new List<string>();
l.Add(#<input id="updated_value2" data-bind="value:value,visible:isEditing()" />);
}
Here's my table
<table class="table table-hover">
<tbody data-bind="foreach: $root.mapJsons(parameters())">
<tr class="data-hover">
<td>
<strong>
<span data-bind="text:key" />
</strong>
</td>
<td>
#*display label and input for dictionary<value> false DIS true APP*#
<input id="updated_value" data-bind="value:value,visible:isEditing()" />
<label id="display_value" data-bind="text:value,visible:!isEditing()" />
</td>
</tr>
</tbody>
<thead>
<tr>
<th style="width: 30%">
Name
</th>
<th style="width: 30%">
Value
</th>
<th></th>
</tr>
</thead>
</table>
Your foreach loop is being executed client-side (looks like a KnockoutJS binding?) rather than server-side, so any Razor code you embed in the table is only going to be called once as it's rendered by the server. So the answer is no, you cannot populate a server-side list with this particular foreach loop.

Linq refer to list of objects which were passed with an include statement

Here is how I pass a model to a view:
public ActionResult Public()
{
db = new Entities();
var regions = db.Regions.Include(d => d.Days);
return View(regions.ToList());
}
Now I want to be able to create a foreach in the view which will access the collection of days I included in regions for that particular region. Is there a way to do this through link or should I create a seperate ViewModel that contains fields separately for Regions and for Days?
part of view code:
...
#for(int i = 1; i < Model.Count(); i ++){
<div id="_page_#i" class="frame" style="display: block;">
<p>
<button style="width:70px" type="button">Add +</button>
</p>
<p>
<table class="table striped bordered hovered" >
<thead>
<tr>
<th class="text-left">Title</th>
<th class="text-left">Date</th>
<th class="text-left"> </th>
</tr>
</thead>
<tbody>
#foreach(var item in Model){
<tr class="">
<td>#item</td>
<td class="right"></td>
<td class="right" style="width: 180px; height: 35px"><button style="width:159px; height: 32px" type="button">Delete -</button></td>
</tr>
}
</tbody>
</table>
</p>
</div>
}
</div>
</div>
</div>
</div>
I need the foreach to refer to the Days in the Regions so that item would be one Day.
In your view you do a foreach within a foreach. In my example, it will creale a list of Days for all Regions... assuming the Day has a property "Name":
<ul>
#foreach( var r in Model){
foreach( var d in r.Days){
<li>d.Name</li>
}
}
</ul>

Categories